[Rails] 123 * 3 => 123123123! But why?

Rodrigo Alvarez Fernández papipo at gmail.com
Thu Jan 19 23:27:32 GMT 2006


you are multiplying a string, not a number. Try using .to_f (to float)
method

On 1/20/06, Joshua Muheim <forum at josh.ch> wrote:
>
> Hi all
>
> I extended the Globalize Plugin the following way:
>
> --------
> module Globalize
>   class Currency
>     def conversion_rate
>       code = Locale.active.currency_code
>       rates = { :USD => 1,
>                 :CHF => 0.75,
>                 :EUR => 1.1,
>                 :GBP => 3.1
>               }
>       value = rates[code.to_sym]
>       raise "No conversion rate found for currency '#{code}'
> (#{Locale.active.language.english_name})!" if value.nil?
>       value
>     end
>
>     def dollar_part
>       na? ? nil : cents / 100 / conversion_rate
>     end
>
>     def cent_part
>       na? ? nil : cents % 100 / conversion_rate
>     end
>   end
> end
> --------
>
> Sadly I get ugly results like
>
> $99.00 # correct!
> SFr. 132,0.00 # incorrect!
> € 90,0,00 # incorrect!
>
> I don't know why Ruby concatenates the values and does not multiply
> them... How can I fix that?
>
> Thanks for help. :-)
> Greetings, Josh
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060119/b337cd58/attachment.html


More information about the Rails mailing list