Author
double value formatting ...
Vassili Vladimir
Ranch Hand
Joined: Mar 08, 2007
Posts: 1585
posted Nov 25, 2008 03:36:00
0
Hi, My program generates this value 1.1574074074074073E-5 I want to format it as 1.15 or 1.16 How can I achieve it ? Thanks,
Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35438
posted Nov 25, 2008 03:59:00
0
That's what the java.text.NumberFormat class does. Its setMaximumFractionDigits method determines how many decimal places are used.
Android apps – ImageJ plugins – Java web charts
Vassili Vladimir
Ranch Hand
Joined: Mar 08, 2007
Posts: 1585
posted Nov 25, 2008 05:15:00
0
I tried it, i did the following: but am getting the value : 0.000 Why am I getting zeros ? Thanks,
Vassili Vladimir
Ranch Hand
Joined: Mar 08, 2007
Posts: 1585
posted Nov 25, 2008 05:17:00
0
sorry, i forgot to mention that i used the format method passing the double value and got the output 0.000
Vassili Vladimir
Ranch Hand
Joined: Mar 08, 2007
Posts: 1585
posted Nov 25, 2008 05:31:00
0
Do i have to use the parse methods with the format ? I did and got the value of 0 !!! Is it because of the E-5 ? If yes, please can you tell me what is the solution ? Thanks,
Vassili Vladimir
Ranch Hand
Joined: Mar 08, 2007
Posts: 1585
posted Nov 25, 2008 05:34:00
0
I think that what's happening is mathematically correct !!!
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
You can also use String.format("%.3f", value) if you're using Java 1.5 or up. But 1.1574074074074073E-5 is [n]not[/b] 1.15 or 1.16 - not even close. It is in fact 0.000011574074074074073 - the E-5 at the end means "multiply by 10^5". And that is why you get 0.000.
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
Originally posted by Rob Prime: . . .the E-5 at the end means "multiply by 10^5".
Surely you meant multiply by 10^minus 5?
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
Surely I did.
subject: double value formatting ...