• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Leading zeros

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I was wondering if there is a way to keep the leading zeros in front of an int e.g
String s1 = "020";
Integer.parseInt(s1);
Ok here's where is gets interesting, take the number 123056789 for instance, if you break down this number into groups of 3 and store them as integers then the number changes completely (056 because only 56 and that makes a big difference). This is the reason why I need to keep that leading zero. I know it's reserved for octal numbers but is there a way around it??
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, use a String.
Why do you want to do this, anyway?
--Mark
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want leading zeroes on numbers when you output them, look the java.text.DecimalFormat class. You can do about anything you want.
Or you can break down your number (123056789) into three integers, but just be careful how you work with them:

The output will be:
12356789
123056789
The first one doesn't preserve the "original" number, the second one does. So you just have to be careful how you use the numbers.
[ October 21, 2003: Message edited by: Wayne L Johnson ]
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No sure what you are trying to accomplish here with the numbers.
From the post, I'm guessing that you have a number, you break it down to 3 digit groups, with maximum value each at 999, and display them with 0 in front if the value of one group is less than 100 (099, 088)?
If this is the case, it would be easy using string manipulation. if values.group(1).length == 2 pad 0, etc.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this is all about inputting and outputting numbers Strings using comma-separated groups of three digits, e.g. converting
12003045 <=> "12,0003,045"
then you probably don't even have to split into groups of three digits - DecimalFormat will do this for you.

If you want additional leading or trailing zeros for some reason, use '0' instead of '#' in the DecimalFormat constructor:

This will print the number as 000,012,003,045.00 if that's what you want.
Now if you really need to do something more complicated, it may still be useful to break this number into separate parts, like 12, 3, 45 - but I suspect that this won't really be necessary, as DecimalFormat can probably already do what you really need here.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it's got to do with OUTPUT but with input.
So textformatter is the solution for a problem we don't have.
If you read a Big Number in Groups - perhaps BigDecimal is a helpful hint.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For input (String to int) use parse(). For output (int to String) use format().
 
author
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really fun!!!
Where is Mart (the original poster of the question)??? He should answer Mark's quesion in the second post.
I presume that the original question has to do with the formatting of output and I agree with Jim Y., regarding the usefulness of the NumberFormat class, 'cause that's what it does best!
Learning Java includes understandng the built-in classes. Thanks Jim!
Regards,
 
I will suppress my every urge. But not this shameless plug:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic