Jump to Real's How-to Main page

Display numbers with leading zeroes

import java.text.*;
public class DemoNumber {
  public static void main(String args[]) {
    long n = 123456;
    String mask = "00000000000";
    // jdk1.1
    DecimalFormat df = new DecimalFormat(mask);
    System.out.println(df.format(n));

    // pre-jdk1.1
    String ds = Long.toString(n);  // double to string
    String z = mask.substring(0 , mask.length() - ds.length()) + ds;
    System.out.println(z);
    }
}

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]