# Remove trailing zeros from currency and qty

**URL:** <https://www.ruby-forum.com/t/remove-trailing-zeros-from-currency-and-qty/143652>\
**Category:** Rails\
**Created:** [July 21, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/remove-trailing-zeros-from-currency-and-qty/143652 "2008-07-21T15:56:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gorgeoussoul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6f9a4e/32.png) [@gorgeoussoul](https://www.ruby-forum.com/u/gorgeoussoul)\
**Post date:** [July 21, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/remove-trailing-zeros-from-currency-and-qty/143652/1 "2008-07-21T15:56:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![gorgeoussoul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6f9a4e/32.png) [@gorgeoussoul](https://www.ruby-forum.com/u/gorgeoussoul)\
**Post date:** [July 22, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/remove-trailing-zeros-from-currency-and-qty/143652/2 "2008-07-22T18:39:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![gorgeoussoul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6f9a4e/32.png) [@gorgeoussoul](https://www.ruby-forum.com/u/gorgeoussoul)\
**Post date:** [July 21, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/remove-trailing-zeros-from-currency-and-qty/143652/3 "2008-07-21T23:15:45Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
