Displaying double digits


#1

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!


#2

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


#3

$ 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


#4

Oops. That second example should be

<%= number_with_precision(number, 2) %>


#5

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.