3 Replies - 5011 Views - Last Post: 29 June 2009 - 09:42 PM Rate Topic: -----

#1 sandeepdvn   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-February 09

Adding leading zeros

Post icon  Posted 29 June 2009 - 09:31 PM

Basically I'm from SAP side.. so i',m not good at java coding,methods..stuff...

I have requirement for adding leading zeros....

If the given input value alphanumeric(Ex SP0000245 ) ..then output should remain same..

Other than alphanumeric value.. (ex 1234,3312) it should be padded with zeros such that it length should become 18 (ex 000000000000001234)

can any one please give me code to achieve this requirement

Thanks in advance

Regards,
Deep

Is This A Good Question/Topic? 0
  • +

Replies To: Adding leading zeros

#2 Dantheman   User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Adding leading zeros

Posted 29 June 2009 - 09:32 PM

No. Sorry.

Figure out the length of the string. That will give you the number of zeroes that you need to add. After that it's just a simple concatenation of strings. For example "0" + "0" produces "00".

This post has been edited by Dantheman: 29 June 2009 - 09:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Adding leading zeros

Posted 29 June 2009 - 09:40 PM

Use System.out.printf("%018d", numberVariable);

Adding a 0 before the length (18) will make it show all leading 0 that are not used in the length of 18.
% starts the formatting "tag" I suppose could call it. 0 adds zeros, 18 is the length before a decimal. You can add a .2 if you want it to round to the 2nd decimal place or whatever number you need, and to "close" the "tag" you add a 'd' which represents the variable type.

Look up printf Formatters for all the formatting options

Hope this helps

This post has been edited by Fuzzyness: 29 June 2009 - 09:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Adding leading zeros

Posted 29 June 2009 - 09:42 PM

or printf("%10.10d", int)

This post has been edited by pbl: 29 June 2009 - 09:43 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1