Help for rounding up decimals

please help me solve this question, i have convert it from watt to
horsepower but rounding it up to [0,2,4] is giving me problem, so what
should i do to solve it.

“Translate the vehicle’s motor power from watts to horsepower (1 hp =
745.7 W). Result type with an accuracy of 0, 2, 4 decimal places.”

D:\usr>irb
irb(main):001:0> “%.2f” % 1.1234
=> “1.12”
irb(main):002:0> “%.2f” % 100001.1234
=> “100001.12”
irb(main):004:0> “%.3f” % 100001.1234
=> “100001.123”
irb(main):005:0> “%.4f” % 100001.1234
=> “100001.1234”
irb(main):006:0>

or something like:
def rounded(f,ndigits) ((f*(10ndigits)).round)/(1.0*10ndigits) end

or see Ruby: Rounding float in Ruby - Stack Overflow

(1.2).round(n)

Sometime, I wonder if stackoverflow can replace all web, for a dev…

Regis d’Aubarede wrote in post #1167592:

D:\usr>irb
irb(main):001:0> “%.2f” % 1.1234
=> “1.12”
irb(main):002:0> “%.2f” % 100001.1234
=> “100001.12”
irb(main):004:0> “%.3f” % 100001.1234
=> “100001.123”
irb(main):005:0> “%.4f” % 100001.1234
=> “100001.1234”
irb(main):006:0>

Frankly, that’s what I’d do, too: just output rounded but not change
values themselves.

Sometime, I wonder if stackoverflow can replace all web, for a dev…

Apparently not for everybody - search fu seems generally on the
decline…