In this tutorial, you will learn how to round a Float or a Double value to 2 decimal places in Java.
1. Using BigDecimal
One of the ways to round a float or a double value is to use BigDecimal class.
BigDecimal bigDecimalDouble = new BigDecimal(doubleValue);
BigDecimal class allows us to set a scale and a rounding mode.
bigDecimalDouble.setScale(2, RoundingMode.HALF_UP);
Where:
- scale – is a number of decimal points,
- RoundingMode.HALF_UP – is a rounding mode used. Other rounding modes are:
- ROUND_UP,
- ROUND_DOWN,
- ROUND_CEILING,
- ROUND_FLOOR,
- ROUND_HALF_DOWN,
- ROUND_HALF_EVEN,
- ROUND_UNNECESSARY
Let’s see how it works.
BigDecimal – Round double to 2 decimal points example
import java.math.BigDecimal; import java.math.RoundingMode; public class RoundDouble { public static void main(String args[]) { double doubleValue = 11.8989; BigDecimal bigDecimalDouble = new BigDecimal(doubleValue); System.out.println(bigDecimalDouble); // Prints 11.8988999999999993661958797019906342029571533203125 BigDecimal bigDecimalWithScale = bigDecimalDouble.setScale(2, RoundingMode.HALF_UP); System.out.println(bigDecimalWithScale); // Prints 11.90 } }
Similarly, you can use BigDecimal to round a float value to 2 decimal points.
BigDecimal – Round float to 2 decimal points example
import java.math.BigDecimal; import java.math.RoundingMode; public class RoundDouble { public static void main(String args[]) { float floatValue = 11.8989f; BigDecimal bigDecimalFloat = new BigDecimal(floatValue); System.out.println(bigDecimalFloat); // Prints 11.89890003204345703125 BigDecimal bigDecimalFloatWithScale = bigDecimalFloat.setScale(2, RoundingMode.HALF_UP); System.out.println(bigDecimalFloatWithScale); // Prints 11.90 } }
2. Using DecimalFormat
Another way to round a float or a double value to 2 decimal points is to use the DecimalFormat class. DecimalFormat class allows us to create an output pattern and then set a rounding mode that we want to apply to a value.
Round to 2 decimal points
DecimalFormat decimalFormat = new DecimalFormat("0.00"); decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
Round to 1 decimal point
DecimalFormat decimalFormat = new DecimalFormat("0.0"); decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
Format Pattern “0.00” vs “#.##”
When creating a DecimalFormat object, there are two ways to set a formatting pattern:
- new DecimalFormat(“0.00”)
- new DecimalFormat(“#.##”)
Both of these formatting patterns work similarly, except that “#.##” will not display a digit if the last decimal point is zero. For example,
if doubleValue = 11.90 then BigDecimal("#.##") will print 11.9
On the other hand, the formatting pattern “0.00” will always display a digit even if it is zero. For example,
if doubleValue = 11.90 then BigDecimal("0.00") will print 11.90
Rounding Modes:
Other possible rounding modes are:
- ROUND_UP,
- ROUND_DOWN,
- ROUND_CEILING,
- ROUND_FLOOR,
- ROUND_HALF_DOWN,
- ROUND_HALF_EVEN,
- ROUND_UNNECESSARY
Let’s have a look at the complete example.
Decimal Format – Round double to 2 decimal points example
import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; public class RoundDouble { public static void main(String args[]) { double doubleValue = 11.8989; DecimalFormat decimalFormat = new DecimalFormat("0.00"); decimalFormat.setRoundingMode(RoundingMode.HALF_UP); System.out.println(decimalFormat.format(doubleValue)); //11.90 } }
Decimal Format – Round float to 2 decimal points example
import java.math.RoundingMode; import java.text.DecimalFormat; public class RoundFloat { public static void main(String args[]) { float floatValue = 11.8989f; DecimalFormat decimalFormat = new DecimalFormat("0.00"); decimalFormat.setRoundingMode(RoundingMode.HALF_UP); System.out.println(decimalFormat.format(floatValue)); //11.90 } }
3. Using String.format(“%.2f”, floatValue)
Alternatively, you can use String.format() function to format the input value to 2 decimal points. Although, String.format() does not allow to change the rounding mode. The only rounding mode applicable is half-up.
public class RoundFloat { public static void main(String args[]) { float floatValue = 11.80f; System.out.println(String.format("%.2f", floatValue)); // Prints 11.80 } }
I hope this tutorial was helpful for you.
There are many other useful tutorials you can find on this site. To find Java-related tutorials, check out the Java tutorials page.
Happy learning!