Different Ways to Format Long with Java System.out.format



The System.out.format is used in Java to format output. Here, let's say the following is our long.

long val = 787890;

To format, we have considered the following that justifies the output.

System.out.format("%d%n", val);
System.out.format("%9d%n", val);
System.out.format("%+9d%n", val);
System.out.format("%08d%n", val);
System.out.format("%,9d%n", val);

The following is the complete example that displays the difference in output as well.

Example

Live Demo

import java.util.Locale;
public class Demo {
   public static void main(String []args) {
      long val = 787890;
      System.out.format("%d%n", val);
      System.out.format("%9d%n", val);
      System.out.format("%+9d%n", val);
      System.out.format("%08d%n", val);
      System.out.format("%,9d%n", val);
   }
}

Here is the output. You can easily notice the differences in format.

Output

787890
   787890
  +787890
00787890
  787,890
Updated on: 2019-07-30T22:30:23+05:30

436 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements