Forum: Ruby on Rails displaying double digits

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
cranberry (Guest)
on 2006-02-21 16:16
Hi,

I have a question about displaying digits in Rails.

whenever I print the price of an item, if the last digit is a zero or
double zeros then it doesn't display properly.

How do I display a number so that it looks like currency?

thank you!
Xavier N. (Guest)
on 2006-02-21 16:22
(Received via mailing list)
On Feb 21, 2006, at 15:16, cranberry wrote:

> Hi,
>
> I have a question about displaying digits in Rails.
>
> whenever I print the price of an item, if the last digit is a zero or
> double zeros then it doesn't display properly.
>
> How do I display a number so that it looks like currency?

There are some helpers that may be useful:

     number_to_currency
     number_with_delimiter
     number_with_precision

See http://api.rubyonrails.org for detais.

-- fxn
Kent S. (Guest)
on 2006-02-21 16:22
(Received via mailing list)
$ irb
irb(main):001:0> i = 15.00
=> 15.0
irb(main):002:0> puts i
15.0
=> nil
irb(main):003:0> puts "%.2f" % i
15.00
=> nil

--
Kent
---
http://www.datanoise.com
Richard W. (Guest)
on 2006-02-21 16:29
(Received via mailing list)
Like this:

<%=h money(number) %>

Of course "number" will be the name of YOUR field.
Or you can do this:

<% number_with_precision(number, 2) %>

where 2 is the number of digits of the fraction to show.
Richard W. (Guest)
on 2006-02-21 16:29
(Received via mailing list)
Oops.  That second example should be

<%= number_with_precision(number, 2) %>
This topic is locked and can not be replied to.