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.
Aa776a6aaefaabca63e989e7042da5e2?d=identicon&s=25 cranberry (Guest)
on 2006-02-21 15: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!
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-02-21 15: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
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2006-02-21 15: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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Richard Williams (Guest)
on 2006-02-21 15: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.
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Richard Williams (Guest)
on 2006-02-21 15: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.