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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Robinson
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> 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!
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---