aspose file tools*
The moose likes Beginning Java and the fly likes formatting to 4 decimal places Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "formatting to 4 decimal places " Watch "formatting to 4 decimal places " New topic
Author

formatting to 4 decimal places

terry oliver
Greenhorn

Joined: Jan 04, 2008
Posts: 22
my problem is that to get the data to a jTextField it had to be cast to string i need to format it to 3 or4 decimal places i cant seem to find anything to do it can anyone give me some guidance.

Thanks
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Terry,

You'll want to use DecimalFormat to get your number to appear in the desired format. An example format to display to the third (or fourth if relevant) decimal might be:



There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
As an alternative for Java5 or Java6 use the static format method of the String class.
. . . String.format("%6.3f", doubleNumber);
terry oliver
Greenhorn

Joined: Jan 04, 2008
Posts: 22
thanks very much worked perfectly saved some of my hair

olly
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
public class formatdemo {

/**
* @param args
*/
public static void main(String[] args)
{

Float myNumber = new Float(4.995);
String myValue = String.format("%1.3f", myNumber);
System.out.println(myValue);
}

}

o/p - 4.995
Can't it roundoff the no.


Thanks<br />Dinesh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Originally posted by Dinesh Tahiliani:
Can't it roundoff the no.
If you pass a number with 3 decimal places and print it with 3 decimal places there won't be any rounding. Try with 4.99999995
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: formatting to 4 decimal places