public static void main(String[] args) {
// TODO Auto-generated method stub
BigDecimal foo,foo1;
foo=BigDecimal.valueOf(3.1);
foo1=BigDecimal.valueOf(3.1f);
System.out.println(foo);
System.out.println(foo1);
}
RESULT:
3.1
3.0999999046325684
Why they are different result? I am using JDK1.7.0_03
3.1defines adoublewhile3.1fdefines afloat. What you see is the problem thefloathas of representing that value (float uses “only” 32-bits and double 64-bits).If you want to define a
3.1exactly usingBigDecimaluse theStringconstructor:Output: