Ruby - Printing zero as decimal place

2

I am learning to program in Ruby, and to go training, I use Uri Online Judge, where I solve exercises that are presented, I make the code in some language that the platform accepts (in this case Ruby) and then I send it to the system , but the output has to be exactly the same as it is displayed, such as displaying two decimal places (two numbers after the period), it can not be anything more or less.

I'm having trouble making Ruby display decimal places.

As for example the number 1051 (I'm having difficulties in other exercises too, but this was the most recent one), 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 have already added the variable with 0.001 but it still displays only 1 decimal place in case of zeros.

How can I resolve this, or is it a bug from Ruby itself?

  

output example submitted by the URI:

    
asked by anonymous 02.05.2017 / 01:15

1 answer

2

You can output the floating point, try:

puts "R$ %0.02f\n" % s.round(2)

instead of

puts "R$ #{s.round 2}\n"

The result for me was ok, if I understood your question well:

4520.00
R$ 355.60

Good luck. EDIT: some more formatting tips

    
02.05.2017 / 16:12