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,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Installing MongoDB on Linux/Unix/macOS/Ubuntu - MacOS
- How to remove or unstage a file from git staged area - Git
- How to Recover Deleted Messages on iPhone - iOS
- Fix: Jupyter Notebook Black Web Page on Localhost - Python
- Linux Remove or Delete Files and Directories using Terminal Commands - Linux
- Android : Unable to load VM from snapshot : Mac OS X Error - Android
- How to Clear Gradle Cache on Mac - Gradle
- Java + Spring JDBC Template + Gradle Example - Java