- 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 format to 2 decimal places in a 10-character field
To format, use the Formatter class. Import the following package to work with the Formatter class in Java −
import java.util.Formatter;
Create a Formatter object and format to 2 decimal places in a 10-character field −
Formatter f = new Formatter(); System.out.println(f.format("%10.2e", 3989.7886));
The following is the complete example −
Example
import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); System.out.println(f.format("%08d", 697)); f = new Formatter(); System.out.println(f.format("%03d", 9878)); f = new Formatter(); System.out.println(f.format("%10.2e", 3989.7886)); } }
Output
00000697 9878 3.99e+03
Advertisements