-3

This question has been answered so please close it... Thanks for the clarifications!!

I looked at the question above but there is an use case which we should consider before closing the issue:

I have a situation where I raise an order and the system generates a reference number as: 0000002443 I store that number as a string. When the system sends the order out, it sends two documents. One as a requisition with the above reference number and the other as a Purchase order with a reference: 0000002444

I need to be able to store the first reference number (i.e. 0000002443) as an Integer keeping the preceding zeroes and add +1 and store as a PO reference number (i.e.0000002444) to verify the orders later.

If I keep the first reference number as a String then I won't be able to add 1 to the reference number to get the PO reference Number.

It's a Follow up question: https://stackoverflow.com/questions/15025136/converting-string-to-integer-but-preceding-zero-is-being-removed

Community
  • 1
  • 1
vijay pujar
  • 1,683
  • 4
  • 19
  • 32
  • Are you not happy with answers over there? What is missing in those answers? – kosa Nov 08 '13 at 14:55
  • It's not a java Integer if it has preceding zeros. You're asking for something impossible. – tom Nov 08 '13 at 14:57
  • The answer from DwB in the other Thread seems quite good. You keep the Integer and use `String.format("%08d", value);` when you need to show or use the whole number. What's wrong with that approach? I would always store the Integer (without preceeding zeroes) and use the formatting just for display. – Martin Nov 08 '13 at 14:59
  • Only Strings store formatted information This means that phone numbers are not integers just because they look like integers. and numbers which have to have a specific number of leading `0` are not integers either. This is because 2443 = 02443 = 002443 = 0000002443, so when you convert it to a string it doesn't know how many zeros you want. – Peter Lawrey Nov 08 '13 at 15:03

2 Answers2

3

Integers do not have leading zeros (as it says in that other question)

You'd need to convert it to an int, add one, and then pad it back into a String:

def ref = '0000002443'

def refPlusOne = "${ref.toInteger() + 1}".padLeft( ref.length(), '0' )
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • I would have answered the same way Tim I guess, but again in case the system starts generating alphanumeric reference numbers instead of only numbers, we have to check whether it is a number in first place as `ref.isNumber()` but again it will not solve the problem. Now I feel the weight of John's last line in answer. :) – dmahapatro Nov 08 '13 at 15:55
2

Simply put, an integer doesn't have a number of leading zeroes. It doesn't even have information about whether it's decimal, hex, or anything like that. It's just an integer.

If you really need to follow your existing design, I suggest you parse it as an integer, add one, and then repad with as many zeroes as you need to get back to the original length.

To be honest, if it's really just meant to be a number, it would be better if you stored it as a number instead of using a string at all.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks everyone. I think I just have to store the number as an integer and increment it. And when required convert it back to string and compare it with destination value. Thanks a lot again. – vijay pujar Nov 12 '13 at 00:40