Search This Blog

Saturday, November 10, 2018

BigDecimal in Java for Currency

BigDecimal value = new BigDecimal(Float.toString(123.4f));
For values other float and double NaN and ±Infinity, this constructor is compatible with the values returned by Float.toString(float) and Double.toString(double). This is generally the preferred way to convert a float or double into a BigDecimal, as it doesn't suffer from the unpredictability of the BigDecimal(double) constructor.


BigDecimal b=new BigDecimal(3.14);
b=b.add(new BigDecimal(3.1));
//b =6.240000000000000213162820728030055761337280273437500

BigDecimal b=new BigDecimal(3.14);
b=b.add(new BigDecimal(3.1f));
//b= 6.239999904632568483719978758017532527446746826171875

BigDecimal b=new BigDecimal(3.14);
b=b.add(new BigDecimal(Float.toString(3.1f)));
//b=6.240000000000000124344978758017532527446746826171875