How do I convert an octal number to decimal in Ruby?

你。 提交于 2019-12-01 21:53:41

Use Ruby's octal integer literal syntax. Place a 0 before your number, and Ruby will convert it to octal while parsing:

v = 013 # => 11
a[v]    # => 61

If the octal number is coming from an outside source like a file, then it is already a string and you'll have to convert it just like you did in your example:

number = gets.chomp # => "13"
v = number.to_i(8)  # => 11
a[v]                # => 61
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!