- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to round number to fewer decimals
Let’s say we need to round number to 3 decimal places, therefore use the following format −
DecimalFormat decFormat = new DecimalFormat("0.000");
Now, format the number −
decFormat.format(37878.8989)
Since we have used the DecimalFloat class above, therefore do import the following package −
import java.text.DecimalFormat;
The following is an example that rounds a number to 3 decimal places −
Example
import java.text.DecimalFormat; public class Demo { public static void main(String[] args) { DecimalFormat decFormat = new DecimalFormat("0.000"); System.out.println("Result = " + decFormat.format(37878.8989)); } }
Output
Result = 37878.899
Let us see another example that rounds a number to 5 decimal places −
Example
import java.text.DecimalFormat; public class Demo { public static void main(String[] args) { System.out.println(String.format("Result = %.5f", 76576.696799)); } }
Output
Result = 76576.69680
- Related Articles
- Java program to round a number
- Java Program to Round a Number to n Decimal Places
- How to round down to 2 decimals a float using Python?
- Java Program to round a double passed to BigDecimal
- Swift Program to Round a Number to n Decimal Places
- Haskell Program to Round a Number to n Decimal Places
- Kotlin Program to Round a Number to n Decimal Places
- C++ Program to Round a Number to n Decimal Places
- How to round a number to n decimal places in Java
- How to Round off a number?
- Java program to convert decimal number to hexadecimal number
- Java Program to convert binary number to decimal number
- Java Program to convert hexadecimal number to decimal number
- Java Program to convert octal number to decimal number
- JAVA Program To Convert Roman Number to Integer Number

Advertisements