ruby 基本数据类型转换 [英] ruby basic data type conversion

查看:41
本文介绍了ruby 基本数据类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ruby​​ 中转换数据类型时遇到了一些奇怪的问题.

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"

但是

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

似乎当数字开头有 0 时,输出并不符合预期.

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.

谢谢

推荐答案

在数字开头有一个 0 会使解释器/编译器将后面的数字解释为八进制数序列(基数为 8)而不是十进制数序列(基数 10).因此,您输入的数字是八进制数,而不是十进制数.

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.

这篇关于ruby 基本数据类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆