Error rounding values ​​using .round in Ruby

The following part of the code works fine in script / console, but returns the following error when I compile it into a ruby ​​script .:

:in `round': wrong number of arguments (1 for 0) (ArgumentError) tf={"ph"=>{0=>1.33333333333333, 1=>1.5}, "fee"=>{0=>1.66666666666667}, "test"=>{0=>1.16666666666667, 1=>1.25}, "what"=>{0=>2.0, 1=>2.0}, "for"=>{0=>1.5}, "is"=>{0=>1.83333333333333, 1=>1.75}} tf.each{|k,v| v.each{|k1,v1| tf[k][k1]=(v1.round(5))}} 

Any ideas? Greetings!

+1
floating-point ruby rounding
source share
2 answers

Float#round seems to work differently in Ruby 1.8 and Ruby 1.9 : in 1.8 it complains about this argument, in 1.9 it returns a float, correctly rounded to the specified number of decimal places.

But, as the article linked in another answer wisely says:

You should consider the reason you are performing a rounding (or equivalent) operation. If for presentation reasons only the best way could be to use a format string instead, and leave the original data intact.

+1
source share

From what it looks like, you should not pass an argument to the round method. You went through 5.
If you try to round it to five decimal places, there is no built-in method for this (which I know about). This is a page that explains how to do this: http://solutions.hans-eric.com/rounding-off-floating-point-numbers-in-ruby

0
source share

All Articles