Real'sHowTo AddThis Feed Button
Custom Search

Arithmetic with doubleTag(s): String/Number


You may find that
System.out.println( 1.33 - 1.3 );
// output :  0.030000000000000027
The unexpected result is coming from the fact that internal floating-point number representation is not well suited for that kind of operation.

The easiest way to solve this limitation is to the BigDecimal class :

import java.math.BigDecimal;
...
System.out.println
   (BigDecimal.valueOf(1.33).subtract(BigDecimal.valueOf(1.3)));

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2013
[ home ]