Skip to content

Instantly share code, notes, and snippets.

@kirtan403
Last active September 17, 2016 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirtan403/16390c2219b692f6af5c8688dcf7f695 to your computer and use it in GitHub Desktop.
Save kirtan403/16390c2219b692f6af5c8688dcf7f695 to your computer and use it in GitHub Desktop.
Understanding the precision in Java and solving it with BigDecimal
import java.math.*;
public class HelloWorld {
public static void main(String[] args) {
// Double precision issue
double d1 = 0.01;
print("d1 = " + d1);
double d2 = 33.33;
print("d2 = " + d2);
double d3 = d2 + d1;
print("d3 = d2+d = " + d3);
double d4 = 33.34;
print("d4 = " + d4);
// Let's work for BigDecimal
BigDecimal bd1 = new BigDecimal(0.01);
print("bd1 = " + bd1.doubleValue());
BigDecimal bd2 = new BigDecimal(33.33);
print("bd2 = " + bd2.doubleValue());
BigDecimal bd3 = new BigDecimal(33.34);
print("bd3 = " + bd3.doubleValue());
BigDecimal bd4 = new BigDecimal(33.33 + 0.01);
print("bd4 = " + bd4.doubleValue());
BigDecimal bd5 = new BigDecimal(String.format("%.2f", 33.33 + 0.01));
print("bd5 = " + bd5.doubleValue());
// Provide values in String in BigDecimal for better precision
// if you already have value in double like shown below
double d11 = 0.1 + 0.1 + 0.1;
print("d11 = " + d11);
BigDecimal bd11 = new BigDecimal(String.valueOf(d11)); // You already lost the precision
print("bd11 = " + bd11.doubleValue());
/* Convert to string as per your needed precision like 2 for
example when you might have lost the precision */
BigDecimal bd12 = new BigDecimal(String.format("%.2f", d11));
print("bd12 = " + bd12);
// Or make sure to do that before addition in double
double d21 = 0.1;
BigDecimal bd21 = new BigDecimal(String.valueOf(d21));
BigDecimal bd22 = bd21.add(bd21).add(bd21);
print("bd21 = " + bd21.doubleValue() + ", bd22 = " + bd22.doubleValue());
}
public static void print(String s) {
System.out.println(s);
}
public static void print(double d) {
System.out.println(d);
}
}
d1 = 0.01
d2 = 33.33
d3 = d2+d = 33.339999999999996
d4 = 33.34
bd1 = 0.01
bd2 = 33.33
bd3 = 33.34
bd4 = 33.339999999999996
bd5 = 33.34
d11 = 0.30000000000000004
bd11 = 0.30000000000000004
bd12 = 0.30
bd21 = 0.1, bd22 = 0.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment