1

I understand this:

35%10 returns 5

but, why does 000000035%10 return 9?

Ruby does the same thing. I checked with irb.

Should I strip the 0s that are padding the number? What is the java function to do that?

David West
  • 2,256
  • 6
  • 32
  • 62
  • 2
    Because it's an octal for 29 in decimal. – Jordão Oct 20 '12 at 01:17
  • I have some ints in this variable that begin with the zeros like that and others that don't. They're all just decimal integers, and none should be octal. How do I make sure every integer is treated as decimal? – David West Oct 20 '12 at 01:19
  • You need octal digits (0..7) to make an octal literal. – Jordão Oct 20 '12 at 01:22

2 Answers2

3

Leading zero in number literal means octal number.

000000035 = 035 = 3*8 + 5 = 29
29 % 10 = 9

See this page for other available formats.

Ivan Nevostruev
  • 28,143
  • 8
  • 66
  • 82
3

When you put the leading zeros followed by octal digits (from 0 to 7), you're creating an octal literal. And since 000000035 in octal is 29 in decimal, its remainder with 10 is 9.

UPDATE

If you have your "octal literal" in a string, simply use Integer.parseInt and you'll get it parsed as a decimal.

assert Integer.parseInt("035") == 35;
Jordão
  • 55,340
  • 13
  • 112
  • 144
  • I understand. And thank you. But I want any integer I have, such as "000030" or "000123" to have no zeros in it before I process it. How can I accomplish that? – David West Oct 20 '12 at 01:23
  • Just don't use leading zeros. – Jordão Oct 20 '12 at 01:29
  • The integers are generated from really basic OCR code. That means that sometimes the generated numbers have leading zeros. That's just the way it is now. – David West Oct 20 '12 at 01:29
  • 1
    http://stackoverflow.com/questions/2800739/how-to-remove-leading-zeros-from-alphanumeric-text this question shows how to use regex to remove them. – Doon Oct 20 '12 at 01:30
  • 1
    You can simply use [`Integer.parseInt`](http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)). By default it parses decimals. – Jordão Oct 20 '12 at 01:33
  • I have to convert it to a string first. `accountIntArray[i] = Integer.parseInt( Integer.toString(accountNumber))%10;`isn't doing what I need... – David West Oct 20 '12 at 01:45
  • I completely rewrote everything, and eventually got it to work with your help. – David West Oct 20 '12 at 03:37