"wrong number of arguments" ArgumentError when using round

I am trying to convert the temperature from Fahrenheit to Celsius:

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 

I use the correct formula to convert Fahrenheit to Celcius:

Celsius = Fahrenheit - 32 / 1.8

However, when I run it in the console, it causes the following error:

`round ': wrong number of arguments (1 to 0) (ArgumentError)

I tried different things, but I don’t understand why this is not working.

+7
ruby rounding
source share
3 answers

In the ruby ​​version prior to 1.9.0 round , arguments are not accepted. It is rounded to the nearest integer (see documentation on floats and round usage )

Use this instead:

 aprox = (x * 100).round() / 100.0 

The whole point of multiplication and division by 100 is the rounding of the last two digits of x.

+11
source share

You do not specify which version of Ruby you are using. This matters because in Rubies prior to 1.9, Float # round did not accept a parameter. In 1.9+ he does.

 >> RUBY_VERSION # => "1.9.2"
 >> pi = 3.141 # => 3.141
 >> pi.round # => 3
 >> pi.round (1) # => 3.1
 >> 3.141.round (1) # => 3.1
+5
source share

activesupport (part of the rails) also gives you Float # round (accuracy)

+2
source share

All Articles