Forums Register Login

formatting to 4 decimal places

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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:

+Pie Number of slices to send: Send
As an alternative for Java5 or Java6 use the static format method of the String class.
. . . String.format("%6.3f", doubleNumber);
+Pie Number of slices to send: Send
thanks very much worked perfectly saved some of my hair

olly
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6800 times.
Similar Threads
Urgent A method in Java which rounds 18 places to 8 places after decimal
Retriving data with four decimal places
BigInteger
SetPrecision?
How to find number of decimal places?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:07:58.