wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NumberFormat.format() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NumberFormat.format()" Watch "NumberFormat.format()" New topic
Author

NumberFormat.format()

Max Campa
Greenhorn

Joined: Oct 31, 2009
Posts: 10
Hi there,

I stepped trough the following code:


When reaching line 13 the format(double d) method returns the String "987.12346" instead of "987.12345"?

Regards,

Max
Max Campa
Greenhorn

Joined: Oct 31, 2009
Posts: 10
Why is the format(double d) method returning "987.12346" with the digit 6 instead of 5?

Thanks,

Max
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

Because last (fifth) number is rounded up. If 5 was followed by digits 0 to 4, result would be 987.12345.
Max Campa
Greenhorn

Joined: Oct 31, 2009
Posts: 10
Thanks Zandis,

where is this rule stated? Does this rule applies to float types too?

So if the double had been 987.123459 the output would be 987.12349.
If the double had been 987.123451 the output would be 987.12345.

I found this document on the Wikipedia about rounding floating point but I dont understand it http://en.wikipedia.org/wiki/Rounding#Types_of_roundings.

Thanks,

Max
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Besides the rounding problem, note that you should not expect a float or double to be able to hold numbers of arbitrary precision. A float has about 6 decimal digits of precision, and a double about 15 digits. Floats and doubles are stored as binary fractions and they can't represent some decimal numbers to exact precision; for example the number 0.1 cannot be stored in a float or double exactly. Some calculations will inevitably lead to rounding errors.

The API documentation of DecimalFormat says:
DecimalFormat provides rounding modes defined in RoundingMode for formatting. By default, it uses RoundingMode.HALF_EVEN.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Max Campa
Greenhorn

Joined: Oct 31, 2009
Posts: 10
Thanks Jesper.
Rob Poulos
Ranch Hand

Joined: Aug 21, 2009
Posts: 49
i think if you require strict floating-point numbers you would need to specify strictfp in either the method or class declaration. wouldnt this give you the exact number? or would you need BigDecimal involved?

dont mind me, i'm new


SCJA, ITIL V3 Foudation, Studying for SCJP6
Max Campa
Greenhorn

Joined: Oct 31, 2009
Posts: 10
Hi Rob,

I just posted the code to understand the rules of rounding double primitive types.

You would probably use BigDecimal.

Max Campa
Greenhorn

Joined: Oct 31, 2009
Posts: 10
Ok so the rules are 0 to 4 rounds down and 5 to 9 rounds up for example:


This would result to 987.12346.

If I use a float primitive type:

This would result to 987.12347.

Why do we get 7 instead of 6. Where do I find these rules in the Java documentation?

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NumberFormat.format()
 
Similar Threads
floating point division
Arithmetic Exception
rounding a double value
Format number
Trouble with double primitive type