Left zero number has weird results

73 Views Asked by At

I was trying do some sum to numbers and have a number with a very left zero and start to get wired results

142 + 3 = 145

but 0142 + 3 = 101

What is the base number data type for ruby ? ( iam using repl 2.6.3 )

1

There are 1 best solutions below

0
On BEST ANSWER

This is covered in Ruby's "Numbers" documentation:

You can use a special prefix to write numbers in decimal, hexadecimal, octal or binary formats. For decimal numbers use a prefix of 0d, for hexadecimal numbers use a prefix of 0x, for octal numbers use a prefix of 0 or 0o, for binary numbers use a prefix of 0b. The alphabetic component of the number is not case-sensitive.

Meditate on this:

0d170 # => 170
0D170 # => 170

0xaa # => 170
0xAa # => 170
0xAA # => 170
0Xaa # => 170
0XAa # => 170
0XaA # => 170

0252  # => 170
0o252 # => 170
0O252 # => 170

0b10101010 # => 170
0B10101010 # => 170

This is very common among programming languages.

If the concept of number bases is foreign, then these might help: