Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

ruby basic data type conversion

I got some weired problem while converting data type in ruby.

Here are some of the outputs that i got in console

0123 and enter gave me output 83
12345600.to_s   #=> "12345600"

but

012345600.to_s gave me #=> "2739072" but i expect "012345600"

Seems like when there is 0 at the beginning of number the output is not as expected.

Can any one explain why this is happening? or provide me a solution so that i can get my expected output.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Having a 0 at the start of a number makes the interpreter/compiler interpret the following digits as an octal number sequence (base 8) not a decimal number sequence (base 10). Therefore the number you entered is an octal number and not decimal.

You can test this on a scientific calculator by putting it into octal mode and then switching to decimal.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...