Localized Floats

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

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.

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

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?

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.

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.
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 :slight_smile:

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.

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

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.

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.