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?
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?
Leading zero in number literal means octal number.
000000035 = 035 = 3*8 + 5 = 29
29 % 10 = 9
See this page for other available formats.
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;