将字符串转换为Ruby中的十进制数字 [英] Convert string to decimal number in ruby

查看:129
本文介绍了将字符串转换为Ruby中的十进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用小数。
在我的程序中,用户需要输入带小数的数字才能转换该数字。

I need to work with decimals. In my program, the user need to put a number with decimals to convert that number.

问题是:如果我尝试将参数转换为a我得到一个没有小数的整数。

The problem is: If I try to convert the argument into a number I get a integer without decimals.

# ARGV[0] is: 44.33

size = ARGV[0]

puts size.to_i
# size is: 44
# :(


推荐答案

调用 to_i ,得到整数。

尝试调用 to_f ,您应该获得浮点数。

Try calling to_f, you should get a float.

有关更多字符串转换方法的信息,在这里查看

For more string conversion methods, look here.

这篇关于将字符串转换为Ruby中的十进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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