• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with number [Urgent]...

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic