Java String Formatting Java This example shows how to append zeros before an integer with String#printf() Syntax: %0nd where n is any positive integer package com.logicbig.example.string;
import java.util.IllegalFormatFlagsException;
public class StringPrintfIntegerZeroPadding {
public static void main(String[] args) { //padding with zeros System.out.printf("[%04d]%n", 9);
//right padding with zeros is not possible try { System.out.printf("[%-04d]%n", 9); } catch (IllegalFormatFlagsException e) { System.out.println(e); } } }
Output[0009] java.util.IllegalFormatFlagsException: Flags = '-0'
|