我想将温度从华氏温度转换为摄氏温度:
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
Run Code Online (Sandbox Code Playgroud)
我使用正确的公式将华氏温度转换为Celcius:
摄氏度=华氏度 - 32/1.8
但是,当我在控制台中运行它时,它会给我以下错误:
`round':错误的参数个数(1表示0)(ArgumentError)
我尝试了不同的东西,但我不明白为什么这不起作用.
Pie*_*ard 11
在1.9.0之前的ruby版本round中不带参数.它舍入到最接近的整数(请参阅有关浮点数和圆形使用的文档)
请改用:
aprox = (x * 100).round() / 100.0
Run Code Online (Sandbox Code Playgroud)
乘以除以100的整点是围绕x的最后两位数.
您没有指定您正在使用的Ruby版本.这有所不同,因为在Bloies之前,1.9 Float#round没有参数.在1.9+它确实.
>> RUBY_VERSION #=> "1.9.2" >> pi = 3.141 #=> 3.141 >> pi.round #=> 3 >> pi.round(1) #=> 3.1 >> 3.141.round(1) #=> 3.1