Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding in java Float.parseFloat

Tags:

java

Given the following code, I would expect it to return "float = 32000.0001". But instead, it returns "float = 32000.0".

System.out.println("float = "+Float.parseFloat("32000.0001"));

Is there something I can do to prevent/control the rounding? I want the full value returned with no rounding.

like image 826
Shane Avatar asked Aug 09 '10 14:08

Shane


People also ask

What is float parseFloat in Java?

The parseFloat() method in Float Class is a built in method in Java that returns a new float initialized to the value represented by the specified String, as done by the valueOf method of class Float. Syntax: public static float parseFloat(String s)

How to round a float to 2 decimals in Java?

DecimalFormat(“0.00”) We can use DecimalFormat("0.00") to ensure the number always round to 2 decimal places. For DecimalFormat , the default rounding mode is RoundingMode. HALF_EVEN , and we can use setRoundingMode(RoundingMode) to set a specified rounding mode.

How do you round a float in Java?

You can round any floating-point numbers in Java unto n places By using either Math. round(), BigDecimal, or DecimalFormat. I personally prefer to use BigDecimal to round any number in Java because of it's convenient API and support of multiple Rounding modes.


4 Answers

A float has only 24 bits of precision, which is insufficient to hold the number of digits in your value. The rounding is not due to the parse but to the size of the number. You must use a double if you require floating point, or use BigDecimal if you need arbitrary precision.

like image 105
Jim Garrison Avatar answered Oct 19 '22 08:10

Jim Garrison


You need to be very careful when using floating point while writing software.
Certain decimal numbers do not have exact base 2 representations is one thing to keep in mind. 0.1, 0.01, 0.001 are some examples. So to represent these number in base 2 it needs to round.

Another issue is that you are dealing with a finite set of bits to represent numbers and may experience other rounding errors. Also the further you get away from 0, the more numbers there are that cannot be exactly represented, and therefore rounding takes place.

There are several other issues. Most are mentioned in the paper below. For a formal treatise see this: http://www.scribd.com/doc/5836/What-Every-Computer-Scientist-Should-Know-About-FloatingPoint-Arithmetic

Do not use floats where you need to know an exact number.

like image 43
Romain Hippeau Avatar answered Oct 19 '22 08:10

Romain Hippeau


If decimal places are of interest to you, I would strongly recommend using BigDecimal instead.

As it is, it's not entirely clear to me (without checking) whether the problem is the parsing from a string or the formatting back to a string.

EDIT: In this case I strongly suspect it's rounding on parsing... given that float has only 7 (guaranteed) significant digits and you're trying to preserve 9.

like image 23
Jon Skeet Avatar answered Oct 19 '22 09:10

Jon Skeet


As others note, float has insufficient precision to hold the full result. The BigDecimal(double) constructor is exact, so it's a handy way to see the representation:

System.out.println(new BigDecimal(32000.0001f)); // as float
System.out.println(new BigDecimal(32000.0001d)); // as double

Which displays this:

32000
32000.00010000000111176632344722747802734375
like image 34
trashgod Avatar answered Oct 19 '22 07:10

trashgod