Format Float to n decimal places Format Float to n decimal places java java

Format Float to n decimal places


You may also pass the float value, and use:

String.format("%.2f", floatValue);

Documentation


Take a look at DecimalFormat. You can easily use it to take a number and give it a set number of decimal places.

Edit: Example


Try this this helped me a lot

BigDecimal roundfinalPrice = new BigDecimal(5652.25622f).setScale(2,BigDecimal.ROUND_HALF_UP);

Result will beroundfinalPrice --> 5652.26