Rounding in java Float.parseFloat

Given the following code, I 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 rounding? I want the full value to be returned without rounding.

+6
java
source share
6 answers

A float has only 24 bits of precision, which is not enough to store the number of digits in your value. Rounding is not related to parsing, but the size of a number. You should use double if you need a floating point, or use BigDecimal if you need arbitrary precision.

+7
source share

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

Another problem is that you are dealing with a finite set of bits to represent numbers and may experience other rounding errors. In addition, the farther you go from 0, the more numbers that cannot be accurately represented, and therefore rounding occurs.

There are a few more questions. Most of them are mentioned in the article 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 the exact number.

+2
source share

If you are interested in decimals, I highly recommend using BigDecimal instead.

Be that as it may, it is not entirely clear to me (without checking) whether the problem is parsing a string or formatting back to a string.

EDIT: In this case, I strongly doubt that it is rounded when parsing ... given that the float has only 7 (guaranteed) significant digits, and you are trying to save 9.

+1
source share

As others have noted, float has insufficient accuracy to produce a complete result. The constructor of BigDecimal(double) is accurate, so this is a convenient way to see the view:

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

What displays this:

 32000 32000.00010000000111176632344722747802734375 
+1
source share

If you want to avoid rounding, use BigDecimal . The primitive types float and double are faster, but they cannot represent many common decimal values.

0
source share

Floats and doubles are limited. They can accurately represent the fractional parts of numbers as the sum of two base fractions. The fractional part can be fully represented as the sum of some rows, such as:

1/2 + 0/4 + 1/8 + 1/16 + ...

Unfortunately, there are a large number of frequently used numbers that can never be fully represented, for example:

1/3, 1/7, 1/9, 5/11, etc.

You should consider whether you can accurately represent the number before worrying about rounding. If this type of accuracy is extremely important, then you need to look for a solution without Float or Non-Double.

There are binary decimal libraries that will accurately perform such calculations. They are usually slower to calculate, because there is no special equipment to speed up their calculations. In addition, they tend to take up more memory because you essentially keep a list of numbers.

After you can present the number you want, you want rounding to be simple. You probably won't even need a rounding solution; because the main reason you are trying to combine in this case is because the float cannot correctly represent your value.

0
source share

All Articles