• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with number [Urgent]...  RSS feed

 
Nick Paralias
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all!

I'm having some trouble keeping the format of a number showing time.
I have a series of time stamps in format 1.525322174 and I want to substract one from another, then print it on file in the same format. I use double, but comes out in a format e.g. 3.221740000001194E-4. Is it possible somehow this number to be printed as 0.000322174 ???
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paralias paralias, please check your private messages. You can see them by clicking My Private Messages.

Also, ease up.

For formatting floating-point numbers, use class java.text.DecimalFormat or use the String.format() method. (Look those up in the API documentation).
 
Nick Paralias
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jesper!
Well I'm trying now to format a double value, here is my code:



but it displays a comma instead of dot:

$ java check
1.385322774
1.385322784
9.99999993922529E-9
0,00000001


Do you have any idea how to fix this?
And sthing else:
The substraction of the first two numbers should result in 0.00000001.
Why printing the double results in 9.99999993922529E-9 and not 1.0e-8
[ September 18, 2008: Message edited by: Nick Paralias ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floating-point arithmetic is very accurate and not very precise. That sort of phenomenon will occur unpredictably from time to time. That is why you don't use floating-point arithmetic for money, nor for loop indices.
There is the classic paper about floating point copied here, and look at our FAQs (no 20) which explain more.

I presume you are getting 0,000001 instead of 0.000001 because your Locale is set to a part of Europe where , is used instead of . (ie anywhere except UK and Ireland).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!