• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

double value formatting ...

 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what the java.text.NumberFormat class does. Its setMaximumFractionDigits method determines how many decimal places are used.
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it,

i did the following:



but am getting the value : 0.000

Why am I getting zeros ?

Thanks,
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that what's happening is mathematically correct !!!
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely I did.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic