- 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
Formatter Specifier for Octal and Hexadecimal in Java
For Formatter, import the following package −
import java.util.Formatter;
Now create a Formatter object like this −
Formatter f1 = new Formatter(); Formatter f2 = new Formatter(); Formatter f3 = new Formatter();
If you want a Format specifier for Octal, use %o −
f3.format("Octal values %o %o %o", 15, 55, 78);
If you want a Format specifier for Hexadecimal, use %x −
f2.format("Hexadecimal values %x %x %x", 24, 98, 110);
The following is an example −
Example
import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f1 = new Formatter(); Formatter f2 = new Formatter(); Formatter f3 = new Formatter(); f1.format("Rank and Percentage of %s = %d %f", "David", 2, 98.5); System.out.println(f1.toString()); f2.format("Hexadecimal values %x %x %x", 24, 98, 110); System.out.println(f2.toString()); f3.format("Octal values %o %o %o", 15, 55, 78); System.out.println(f3.toString()); } }
Output
Rank and Percentage of David = 2 98.500000 Hexadecimal values 18 62 6e Octal values 17 67 116
Advertisements