Forum: Ruby on Rails Localized Floats

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.
mannl (Guest)
on 2005-11-19 19:08
(Received via mailing list)
Hi!


I stumbled onto one small problem I can't find an easy solution for.

I'm developing an app in german, and here in Germany our Float
numbers are separated by a comma instead of a dot. I.e.: 150,00
instead of 150.00

I need to make the text_field()'s display a comma instead of a dot,
AND make them feed back into the attribute correctly when I POST..

Has anyone ever done this, or know how this could be achieved?


Robert
listbox (Guest)
on 2005-11-19 20:38
(Received via mailing list)
On 19-nov-2005, at 18:05, Robert wrote:

> AND make them feed back into the attribute correctly when I POST..
>
> Has anyone ever done this, or know how this could be achieved?


This is an interesting topic for Multilingual actually.
mannl (Guest)
on 2005-11-19 20:43
(Received via mailing list)
I found a solution. However, I have to do that for every single
attribute that is a float. If anyone has any idea how I could
refactor that, I'd greatly appreciate it. Here it goes:

   # buy_price
   def buy_price=(value)
     write_attribute(:buy_price, value.gsub(',', '.'))
   end

   def buy_price()
     read_attribute(:buy_price).to_s.sub('.', ',')
   end

   # rent_price
   def rent_price=(value)
     write_attribute(:rent_price, value.gsub(',', '.'))
   end

   def rent_price()
     read_attribute(:rent_price).to_s.sub('.', ',')
   end
Simon.Santoro (Guest)
on 2005-11-19 20:52
(Received via mailing list)
Julian 'Julik' Tarkhanov wrote:
> This is an interesting topic for Multilingual actually.

This is a bit ot, but can multilingual create po files instead of ruby
code? That would be great, since we use some very nice po translation
ides that make our transrators work very easy and fast.
listbox (Guest)
on 2005-11-19 21:19
(Received via mailing list)
On 19-nov-2005, at 19:43, Robert wrote:

>     read_attribute(:buy_price).to_s.sub('.', ',')
>   end
>
>   # rent_price
>   def rent_price=(value)
>     write_attribute(:rent_price, value.gsub(',', '.'))
>   end
>
>   def rent_price()
>     read_attribute(:rent_price).to_s.sub('.', ',')
>   end

Uhm. Are you sure that it will work if you try to calculate with
these floats using Ruby?
mannl (Guest)
on 2005-11-19 21:54
(Received via mailing list)
>> I found a solution. However, I have to do that for every single
>> attribute that is a float. If anyone has any idea how I could
>> refactor that, I'd greatly appreciate it. Here it goes:
>> [snip]
>
> Uhm. Are you sure that it will work if you try to calculate with
> these floats using Ruby?
No, that won't work.. But I can access them with object[:buy_price]
instead... It's far from perfect, but I needed a solution..
listbox (Guest)
on 2005-11-20 00:28
(Received via mailing list)
On 19-nov-2005, at 20:53, Robert wrote:

>>> I found a solution. However, I have to do that for every single
>>> attribute that is a float. If anyone has any idea how I could
>>> refactor that, I'd greatly appreciate it. Here it goes:
>>> [snip]
>>
>> Uhm. Are you sure that it will work if you try to calculate with
>> these floats using Ruby?
> No, that won't work.. But I can access them with object[:buy_price]
> instead... It's far from perfect, but I needed a solution..

Why don't you try to override text_field and rewrite params when
recieving them instead? I think your problem is much better to be solved
on the view/controller side that on the model side.
mannl (Guest)
on 2005-11-20 00:41
(Received via mailing list)
> Why don't you try to override text_field and rewrite params when
> recieving them instead? I think your problem is much better to be
> solved
> on the view/controller side that on the model side.
You are right. Somehow rewriting the params is a good idea, I will
try that tomorrow


> My Multilingual plugin will handle this by overriding Float#to_s
> depending on what your locale is set to. (You set the locale with e.g.
> Locale.set "de-DE".)
>
> I'll be putting up an announcement in the next couple of days. Feel
> free to contact me for more details.
Sounds interesting too, though I'm not so keen on tingling with Ruby
that deep down. I'm looking forward to the release :)
Andreas S. (Guest)
on 2005-11-20 00:50
mannl wrote:

> I'm developing an app in german, and here in Germany our Float
> numbers are separated by a comma instead of a dot. I.e.: 150,00
> instead of 150.00

Btw:
Floats are unsuited for monetary values and some other things, even
though they are often misused for it. Most of the time it won't matter,
but in some cases it will, therefore it's better to do it right in the
first place and use BigDecimal.
bpaland (Guest)
on 2005-11-20 00:55
(Received via mailing list)
Hi Robert,

I just wrote an helpermethod:

  def to_EUR(price)
    return sprintf( "%.2f €", price ).sub( /\./, ',')
  end

and kept the floats in the database as normal with dots.

HTH,
Beate
norman (Guest)
on 2005-11-20 00:55
(Received via mailing list)
Am Samstag, den 19.11.2005, 18:05 +0100 schrieb Robert:
> AND make them feed back into the attribute correctly when I POST..
>
> Has anyone ever done this, or know how this could be achieved?
>

Have a look at this rough l10n class i wrote and attached to this mail.
It provides some .l10n_to_s and .l10n_format methods for basic classes
like Numeric and Date. It's far from being complete.

Overwriting the .to_s method is not always a good idea, because some
external code might rely on the original form and behaviour.
This topic is locked and can not be replied to.