![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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
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
Advertisements