我正在尝试将温度从华氏度转换为摄氏度:
puts 'Convertir grados Fahrenheit a Celcius'
STDOUT.flush
x = gets.chomp
aprox = (x * 100.0).round(2) / 100.0
resultado = (aprox-32)/1.8
puts resultado
我使用正确的公式将华氏度转换为摄氏度:
Celsius = Fahrenheit - 32 / 1.8
但是,当我在控制台中运行它时,出现以下错误:
`round': wrong number of arguments (1 for 0) (ArgumentError)
我尝试过不同的方法,但我不明白为什么这不起作用。
最佳答案
在 1.9.0 之前的 ruby 版本中,round
不带参数。它四舍五入到最接近的整数(参见 documentation about floats and the use of round )
改用这个:
aprox = (x * 100).round() / 100.0
乘以和除以 100 的全部意义在于将 x 的最后两位四舍五入。
关于ruby - "wrong number of arguments"使用圆形时出现 ArgumentError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3917274/