In order to round up a number up to 2 decimal places you can make use of DecimalFormat class from java.text package,
Example 1: Convert float value to 2 decimal places
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float noFloat = 10.2123f;
float roundedTo2Float = Float.parseFloat(decimalFormat.format(noFloat));
System.out.println("Before: "+ noFloat);
System.out.println("After : "+ roundedTo2Float);
}
Output:
Before: 10.2123
After : 10.21
Example 2: Convert double value to 2 decimal places
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("#.##");
double noDouble = 2022.126124;
double roundedTo2Double = Double.parseDouble(decimalFormat.format(noDouble));
System.out.println("Before: "+ noDouble);
System.out.println("After : "+ roundedTo2Double);
}
Output:
Before: 10.2123
After : 10.21
Example 3: Convert BigDecimal value to 2 decimal places
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("#.##");
BigDecimal bigDecimal = new BigDecimal(10.12345);
BigDecimal roundedTo2bigDecimal = new BigDecimal(decimalFormat.format(bigDecimal));
System.out.println("Before: "+ bigDecimal);
System.out.println("After : "+ roundedTo2bigDecimal);
}
Output:
Output:
Before: 10.1234500000000000596855898038484156131744384765625
After : 10.12
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- Android Studio - Installed build tools revision 31.0.0 is corrupted. Remove and install again using the SDK manager. - Android-Studio
- 9 Mac Keyboard Shortcuts for Taking Screenshots (covers macOS Ventura 13) - MacOS
- Convert Instant timestamp into LocalDateTime Java Code Example - Java
- Write JSON to file in pretty-printed Format using Java Jackson - Java
- How to insert image into Google Sheets cell - Google
- [jQuery] Uncaught ReferenceError: $ is not defined at index.html:5 - jQuery
- Fix - bash: man: command not found - Linux
- [Solved] Bootstrap tooltip not working - Bootstrap