2
I’m learning to code in Ruby, and to train, I use Uri Online Judge, where I solve exercises that are presented, code in some language that the platform accepts (in this case Ruby) and then send to the system, but the output has to be exactly the same as that shown, for example, display two decimal places (two numbers after the point), can be neither more nor less.
I’m having a hard time getting Ruby to display decimal place zeros.
As for example the number 1051 (I’m having difficulties in other exercises too, but this was the most recent), the code is:
a = 1000 * (8/100.0)
b = 1500 * (18/100.0)
s = gets.to_f
if s >= 0 and s <= 2000
puts "Isento"
elsif s > 2000 and s <= 3000
s -= 2000
s *= 8/100.0
puts "R$ #{s.round 2}\n"
elsif s > 3000 and s <= 4500
s -= 3000
s *= 18/100.0
s += a
puts "R$ #{s.round 2}\n"
elsif s > 4500
s -= 4500
s *= 28/100.0
s += a + b
puts "R$ #{s.round 2}\n"
end
It should display two decimal places, but when the output would/should display decimal places with "0", it does not display. I already added the variable with 0.001 but even so, displays only 1 decimal place in case of zeros.
How can I fix this, or is it a Ruby bug itself?
example of output presented by the URI:
It worked.. I didn’t know you had this way of printing Ruby, knew the people I introduced and
puts "R$ " + s.to_s
– n3h
@n3h heheh, because eh. This form remember that used a lot in C, very useful for these case too. Good studies.
– Luiz Carvalho
yes, I also did the same exercises in C, and it’s really good looking (
printf("%.2f", s);
). thanks for the guidance..– n3h