How can I convert a BigDecimal to float, having 2 decimal in java?
BigDecimal x=new BigDecimal(any exponential term);
Now I want to convert to float having 2 decimal point only, for example -0.45.
format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.
BigDecimal. floatValue() converts this BigDecimal to a float. If this BigDecimal has too great a magnitude represent as a float, it will be converted to Float.
You can use setScale to round number to any given decimal places.
BigDecimal number = new BigDecimal(2.36359);
float rounded = number.setScale(2, RoundingMode.DOWN).floatValue();
System.out.println(rounded); // prints "2.36"
Once you have the BigDecimal. Use x.floatValue() to compute float and then pass it through Math.round() to round it to 2 digits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With