- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Precision on a number format in Java
You can include a precision specifier to the following format specifiers −
%f %e %g %s
On floating point, the number of decimal places is known.
Let’s say we declared a Formatter object −
Formatter f1 = new Formatter();
Now, we want 3 decimal places. For that, use 1.3f −
f1.format("%1.3f", 29292929.98765432);
The above will return the number with 3 decimal places −
29292929.988
The following is the final example −
Example
import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f1, f2, f3; f1 = new Formatter(); f1.format("%1.3f", 29292929.98765432); System.out.println(f1); f2 = new Formatter(); f2.format("%1.7f", 29292929.98765432); System.out.println(f2); f3 = new Formatter(); f3.format("%1.9f", 29292929.98765432); System.out.println(f3); } }
Output
29292929.988 29292929.9876543 292929.987654320
- Related Articles
- Set width and precision to format output in Java
- Precision String Format Specifier In Swift
- Precision Handling in Java
- Format floating point number in Java
- Parse and format a number to octal in Java
- Parse and format a number to decimal in Java
- What are number format exceptions in Java?
- Java Program to Parse and Format a Number into Binary
- Get maximal GPS precision on mobile browser
- Format Minutes in mm format in Java
- Format Month in M format in Java
- Format Seconds in s format in Java
- Format Year in yy format in Java
- Format Year in yyyy format in Java
- Format Month in MM format in Java

Advertisements