Format octal and hexadecimal value
In this chapter you will learn:
- What specifiers to use to format octal and hexadecimal value
- How to format floating-point values in hexadecimal format
%o and %x are for octal or hexadecimal value format respectively
You can display integers in octal or hexadecimal format by
using %o
and %x
, respectively.
For example, this fragment:
import java.util.Formatter;
// j a va 2 s . c om
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("Hex: %x, Octal: %o", 196, 196);
System.out.println(fmt);
}
}
The output:
Floating-point values in hexadecimal format
You can display floating-point values in
hexadecimal format by using %a
.
%a
uses a form similar to scientific
notation that consists of a significant and an exponent, both in
hexadecimal. Here is the general format:
import java.util.Formatter;
/* j a v a 2 s .c o m*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("%a", 196.123);
System.out.println(fmt);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- What specifiers to use to format time and date
- How to show month by name and number
- How to display standard 12-hour time format
- How to display complete time and date information
- How to just display hour and minute
- How to display day of month as a decimal
Home » Java Tutorial » String