Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Leading zeros  RSS feed

 
Rohit Dhodapkar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain how to retain leading zeros
ex..
int i = 000123.
Interger xyz = new Integer("i");
System.out.println(xyz);
The output is 123.
How do I retain these leading zeros
 
Shaliey Gowtham
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly no way to do this, as any integer with leading zero is generally a octal number.
Can anyone help with any of the format options available?
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe store it as a string and use the string value? Then you can convert it into an int when need be.
But why would you want to do that? I'm not sure if there exists a possibility of storing with leading zeros. Pls tell us the reason for such a need.
Also remember that a leading zero means that the number is treated as a octal, not a decimal.
Sashi
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not in the objectives of the SCJP, so I am going to move this to the Java In General (Beginner) forum.

But, the answer is to store it as a String and use substring and length. Start with a String with the exact number of digits say 5 for example. The String to start off with is "00000", then do a substring on it starting at the beginning and going as far as (5 - (the length of your number)) and concatenate it with your value as a String.

Mark
 
Rohit Dhodapkar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sasikanth Malladi & Shaliey Gowtham ..

Actually a third party system is returning a n Integer object which we need to convert into an string and display on a JSP. But we are loosing thoese leading zeros. We are helpless becoz we cant change the third party response and are left with the Integer object.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rohit Dhodapkar:

int i = 000123;
Interer xyz = new Integer("i");
System.out.println(xyz);
The output is 123.

Really?!
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the quotes around "i" were intentional...

If you're looking to impose a fixed size on your integer strings, padding on the left with 0s, consider the java.text.NumberFormat class. The following snippet should show you how:This outputs "000123" from an Integer wrapping 123. NumberFormat has much more advanced features as well, but this basic one will probably suit your needs (although I'm not absolutely clear what those are).
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charles Lyons:
I don't think the quotes around "i" were intentional...


Even without the quotes, the result is 83, not 123
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even without the quotes, the result is 83, not 123

Yes, okay, it's in octal - but that isn't really the essence of this problem (although perhaps a valuable lesson to learn!).
 
Rohit Dhodapkar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Charles Lyons

That has really helped and solved my problem.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the result is a compiler error. Unless of course Interger is a real class that extends Integer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!