Results 1 to 2 of 2
  1. #1
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    10

    Default How to display numbers with leading zeros

    This program displays numbers with leading zeros.

    Java Code:
    public class number {
    
     	public static void main(String args[]) {
     		long n = 123456789;
     		String mask = "00000000000000";
     		String ds = Long.toString(n); 
    
                    // double to string
     	        String z = mask.substring(0 , mask.length() - ds.length()) + ds;
     	        System.out.println(z);
           }
    }

  2. #2
    michal.zenon is offline Member
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

    Default

    This one too.

    Java Code:
    public class Number {
    public static void main(String args[]) {
        long n = 123456789;
        String f = "%014d";
        String z = String.format(f, n);
        System.out.println(z);
      }
    }

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 04:03 PM
  2. Replies: 2
    Last Post: 03-30-2008, 09:38 PM
  3. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:56 AM
  4. How to display numbers with leading zeros
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:34 PM
  5. how to swap 2 numbers
    By mary in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 06:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •