This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes double value formatting ... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "double value formatting ..." Watch "double value formatting ..." New topic
Author

double value formatting ...

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
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
    
    9
That's what the java.text.NumberFormat class does. Its setMaximumFractionDigits method determines how many decimal places are used.


Android appsImageJ pluginsJava web charts
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
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
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
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
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
    
    4
Originally posted by Rob Prime:
. . .the E-5 at the end means "multiply by 10^5".

Surely you meant multiply by 10^minus5?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

Surely I did.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: double value formatting ...
 
Similar Threads
Tricky Question
how to reset the field after validation using annotations(Struts 2)
How to convert the for Loop to the equalent JSTL code in JSP
Doubt in EL Implicit Objects: Cookie
jsp bean problem