Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Limiting decimals displayed in a simple Java app

Limiting decimals displayed in a simple Java app
Thread Tools
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Mar 5, 2001, 10:55 AM
 
I am a beginner programmer writing a Java app that does matrix math and displays float values in NSTextFields. I was wondering how one sets the number of decimals displayed. I would like the NSTextFields to display only 1 or 2 decimals (e.g. 456.23). Can anyone help me out?

Thank you.
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 5, 2001, 11:16 AM
 
A different but related question, to add.

Is there any way to get the Java3D API classes in for either OS9 or X? I have a Prof who has us using this for the matrix and vector methods. I am also having the same precision issues and my Java class isn't until next quarter.
     
Dedicated MacNNer
Join Date: Oct 2000
Location: Pasadena, CA, USA
Status: Offline
Reply With Quote
Mar 5, 2001, 11:21 AM
 
There may be a way in IB to configure the text field for the behavior you want, but you could also use a java.text.DecimalFormat, like this

DecimalFrom df = new DecimalFormat("$##,###,##0.00;$-##,###,##0.00");
System.out.println(df.format(12345.6789); // outputs "$12,345.68"
System.out.println(df.format(12345)); // outputs "$12,345"

     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Mar 5, 2001, 10:04 PM
 
Thank you! One last question. Do you know how to display large floats in a format similar to: 2.33E34 (i.e. 2.33 x 10^34)?
     
Dedicated MacNNer
Join Date: Oct 2000
Location: Pasadena, CA, USA
Status: Offline
Reply With Quote
Mar 6, 2001, 11:39 AM
 
java.text.DecimalFormat to the rescue again:

> java.text.DecimalFormat df = new java.text.DecimalFormat("#0.##E0");
> System.out.println(df.format(12345.6789)); // outputs "1.235E4"
> System.out.println(df.format(12345)); // outputs "1.234E4"

The java.text. * Format classes are pretty flexible; they're part of the standard JDK and have been around for quite a while.
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Mar 6, 2001, 10:39 PM
 
Wow! Thank you! It works great now.

Originally posted by eyadams:
java.text.DecimalFormat to the rescue again:

> java.text.DecimalFormat df = new java.text.DecimalFormat("#0.##E0");
> System.out.println(df.format(12345.6789)); // outputs "1.235E4"
> System.out.println(df.format(12345)); // outputs "1.234E4"

The java.text. * Format classes are pretty flexible; they're part of the standard JDK and have been around for quite a while.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:31 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2