Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes double value formatting ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
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: 41820
    
  62
That's what the java.text.NumberFormat class does. Its setMaximumFractionDigits method determines how many decimal places are used.


Ping & DNS - my free Android networking tools app
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: 19693
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
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: 19693
    
  20

Surely I did.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: double value formatting ...