![]() |
VOOZH | about |
Format specifiers begin with a percent character (%) and terminate with a "type character, " which indicates the type of data (int, float, etc.) that will be converted the basic manner in which the data will be represented (decimal, hexadecimal, etc.) The general syntax of a format specifier is
% [flags] [width] [.precision] [argsize] typechar
The format() method of Formatter class accepts a wide variety of format specifiers. When an uppercase specifier is used, then letters are shown in uppercase. Otherwise, the upper- and lowercase specifiers perform the same conversion.
| Format Specifier | Conversion Applied |
|---|---|
| %% | Inserts a % sign |
| %x %X | Integer hexadecimal |
| %t %T | Time and Date |
| %s %S | String |
| %n | Inserts a newline character |
| %o | Octal integer |
| %f | Decimal floating-point |
| %e %E | Scientific notation |
| %g | Causes Formatter to use either %f or %e, whichever is shorter |
| %h %H | Hash code of the argument |
| %d | Decimal integer |
| %c | Character |
| %b %B | Boolean |
| %a %A | Floating-point hexadecimal |
Formatter().format("% d", -111);
Formatter().format("% d", 111);
Output:
-111
111
Example:
-111 111 -222 222
Formatter().format("%+d", 111);
Output:
+111
Example:
+111 -111
Formatter().format("%(d", -111);
Formatter().format("%(d", 111);
Output:
(111)
111
Example:
(111) 111
Formatter().format("%, d", 1000000);
Output:
1, 000, 000
Example:
1, 000, 000 32, 659, 526, 566.452
Formatter().format("|%-20.4f|", 1234.1234);
Output:
| 1234.1234|
|1234.1234 |
Example:
| 1234.1234| |1234.1234 |
Geeks For Geeks
10 % 4 = 2
LowerCase Hexadecimal using %x: fa UpperCase Hexadecimal using %X: FA
LowerCase Scientific Notation using %e: 1.231234e+02 UpperCase Scientific Notation using %E: 1.231234E+02
Scientific notation to 2 places: 1.23e+02 Decimal floating-point notation to 4 places: 123.1235 Scientific or Decimal floating-point notation to 4 places: 123.1 String notation to 15 places: 123456789012345 value in 10 digits: 0000000088
Related Article : Format Specifiers in C