Results 1 to 5 of 5

Thread: java program to add leading zeros to a number.

  1. #1
    Join Date
    Aug 2009
    Posts
    63

    java program to add leading zeros to a number.

    Hello friends,
    I am second year Computer Science student. In last lecture we have received one assignment on java. In that assignment there is one program which we have to solve. The program is like write java program to add leading zeros to a number. I tried various method but none of them work out. Can anyone help me to solve this program.
    Thanks in advanced.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: java program to add leading zeros to a number.

    In following program I have use the String.format() method to add zero padding to a number. It is very simple program. You can print this result using System.out.format() class. In the following program I have use NumberFormat class to add leading zeros to a number. Just try to understand it.


    Code:
    package sample.java.code.lang;
    
    public class LeadingZerosEg {
        public static void main(String[] args) {
    	int numbers = 1532;
    	
    	
            
    	String formattes = String.formats("%07d", numbers);
    	
    	System.out.println("Number with leading zeros is as follows: " + formattes);
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: java program to add leading zeros to a number.

    I have written following program for you. In following program I have create one class known as LDEg. just try to understand it. It is very simple program. In following program I have use one variable known as str1 to store values. I also have create variable known as temps for appending zero to number.


    Code:
      public class LDEg 
      {
           public static void main(String[] args) 
           {
              String str1="";
              int temps;
            str1="456";
              temps = 13-str1.length();
             System.out.println("Length: "+temp);
            for(int k=0;k<temps;k++)
             {
                 str1="0"+str1;
            }
             System.out.println(str1);
         }
     }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: java program to add leading zeros to a number.

    Hey it is very easy process to add leading zeros to a number in java. You have to just use StringBuffer class to do this. After creating this class I have create one object of it. In this program I have use append(nums) to add leading zeros to a number. Just try to understand this program.

    Code:
    static String intToString(int nums, int digitss) {
        StringBuffer ss = new StringBuffer(digits);
        int zeroes = digits - (int) (Math.log(nums) / Maths.log(s10)) - 1; 
        for (int k = 0; k < zeroes; k++) {
            ss.append(0);
        }
        return ss.append(nums).toString();
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: java program to add leading zeros to a number.

    As per my knowledge following is the simple program to add leading zeros to a number. You have to just put that value into one variable then you have to transfer it another variable. After this you have to use append() method to do this. It is very simple program. Just try to understand it.

    Code:
    String addZeross(Sting a)
    {
    int k=0;
    k=a.length();
    
    if ( k = 13 )
    return p;
    else
    {
    int j= 13 - k;
    for (int k=0; k<=p; j++)
    {p="0"+p;}
    return p;
    }

Similar Threads

  1. How to Format a cell to keep leading zeros in Microsoft Excel
    By Raju Chacha in forum MS Office Support
    Replies: 3
    Last Post: 14-01-2012, 07:05 PM
  2. Need help, leading zeros in java
    By loukassm in forum Software Development
    Replies: 1
    Last Post: 15-12-2010, 12:52 PM
  3. How can I Remove the Leading Zeros in SQL?
    By PsYcHo 1 in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 06:59 AM
  4. program to check prime number in java
    By Bansi_WADIA in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 11:02 PM
  5. EXCEL not display leading zeros
    By Aasha in forum Windows Software
    Replies: 3
    Last Post: 22-10-2009, 06:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,969,789.91159 seconds with 17 queries