Round up a float with 2 decimal places in Java
This post will discuss how to round up a double with 2 decimal places in Java.
1. Using Math.round() method
The Math.round() method returns the value of the specified double rounded to the nearest long value. The following solution demonstrates its usage to round up a double with 2 decimal places. Note that the number of zeros indicates the number of decimals.
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { double random = Math.random(); System.out.println(random); double roundOff = Math.round(random * 100.0) / 100.0; System.out.println(roundOff); } } |
Note that the number of zeros indicates the number of decimals. Therefore, to round upto 3 decimal places, do like:
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { double random = Math.random(); System.out.println(random); double roundOff = Math.round(random * 1000.0) / 1000.0; System.out.println(roundOff); } } |
Here’s an alternative, equivalent version of the above code:
|
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { double random = Math.random(); double roundOff = (double) Math.round(random * 100) / 100; System.out.println(roundOff); } } |
The floating-point arithmetic can be very tricky, and this method also does not work as desired always. For instance, the value 296.335 gets rounded down to 296.33 instead of 296.34.
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { double random = 296.335; System.out.println(random * 100.0); // 29633.499999999996 System.out.println(Math.round(random * 100.0) / 100.0); // 296.33 } } |
2. Using DecimalFormat.format() method
The idea here is to create a DecimalFormat using the specified pattern and call the DecimalFormat.format() method to get the formatted string. To restrict the double to 2-decimal points, you can use the pattern #.##. You can also set the RoundingMode using the setRoundingMode() method. This approach suffers the same problem as the first approach. i.e, the value 296.335 gets rounded down to 296.33 instead of 296.34.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.math.RoundingMode; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { double random = Math.random(); DecimalFormat df = new DecimalFormat("#.##"); df.setRoundingMode(RoundingMode.FLOOR); String roundOff = df.format(random); System.out.println(roundOff); } } |
3. Using String.format() method
Following is another approach that returns a formatted string from a double value using the String.format() method.
|
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { double random = Math.random(); String roundOff = String.format("%.2f", random); System.out.println(roundOff); } } |
4. Using BigDecimal class
Finally, you can convert the double value to a BigDecimal and scale it using the setScale() method. This approach suffers the same concern as seen in some of the earlier approaches. i.e, the value 296.335 gets rounded down to 296.33 instead of 296.34.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.math.BigDecimal; import java.math.RoundingMode; public class Main { public static void main(String[] args) { double random = Math.random(); BigDecimal bd = new BigDecimal(random); BigDecimal roundOff = bd.setScale(2, RoundingMode.FLOOR); System.out.println(roundOff); } } |
That’s all about rounding up a float with 2 decimal places in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)