Remove trailing zeros from currency and qty

I’m storing qty and currency data in MySQL Decimal(12,3) fields.

Most of the time, people are entering Qty = 1. Occasionally, they’re
entering Qty = 1.25 though.

If the value is 1, I’d like to display Qty = 1 rather than Qty = 1.000.

And the same situation is true for the currency field. Sometimes they
enter a price with 3 decimal places, but most of the time, it’s a
standard x.xx format. So, I’d like to display it the way they’d expect
to see it: $x.xx not $x.xxx.

Any idea how to do it?

Thank you!

Thanks for the reply, Jeff. I can read most of what you wrote, but I’m
not sure what this bit does:

qty = [1, 2.0, 3.00, 4.000, 5.001, 6.010, 7.100]
price = [1, 2.0, 3.00, 4.001, 5.987, 6.010, 7.100]

Should that be in the model, controller, or view?

here’s one way:

def fix_qty(qty)
if qty.to_i == qty
return “%i” % qty
else
return “%.3f” % qty
end
end

def fix_price(price)
return “%.2f” % price
end

qty = [1, 2.0, 3.00, 4.000, 5.001, 6.010, 7.100]
price = [1, 2.0, 3.00, 4.001, 5.987, 6.010, 7.100]

qty.each {|q| puts fix_qty(q)}
price.each {|p| puts fix_price(p)}

On Jul 21, 9:56 am, Denise R. [email protected]