Java - Integral Numeric Formatting

Introduction

The integral number formatting is for whole numbers.

It applies to format values of byte, Byte, short, Short, int, Integer, long, Long and BigInteger.

The following table lists conversions that are available under integral number formatting category.

Conversion
Uppercase variant
Description
'd'

n/a

It formats the argument in locale-specific decimal integer (base-10).
The '#' flag cannot be used with this conversion.
'o'


n/a


It formats the argument as a base-8 integer without any localization.
If '#' flag is used with this conversion, the output always begins with a '0'.
'(' , '+', ' ', and ' ,' flags cannot be used with this conversion.
'x'




'X'




formats the argument as a base-16 integer.
If '#' flag is used, the output always begins with a "0x".
When uppercase 'X' is used with '#' flag, the output always begins with "0X".
'(' , '+' , ' ', and ' ,' flags cannot be used with this conversion with an argument of byte, Byte, short, Short, int, Integer, long, and Long data types.
The ' ,' flag cannot be used with BigInteger data type.

The general syntax for a format specifier for integral number formatting is as follows:

%<argument_index$><flags><width><conversion>

precision is not applicable to integral number formatting.

The following code demonstrates the use of the 'd' conversion with various flags to format integers:

Demo

public class Main {
  public static void main(String[] args) {
    System.out.printf("'%d' %n", 2028);
    System.out.printf("'%6d' %n", 2028);
    System.out.printf("'%-6d' %n", 2028);
    System.out.printf("'%06d' %n", 2028);
    System.out.printf("'%(d' %n", 2028);
    System.out.printf("'%(d' %n", -2028);
    System.out.printf("'% d' %n", 2028);
    System.out.printf("'% d' %n", -2028);
    System.out.printf("'%+d' %n", 2028);
    System.out.printf("'%+d' %n", -2028);
  }/*w  w w. j  a  va  2  s .  com*/
}

Result

Related Topics