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:> 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?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
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.
On 19-nov-2005, at 19:43, 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: > > # 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(''.'', '','') > endUhm. Are you sure that it will work if you try to calculate with these floats using Ruby?
>> 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..
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. Josh Harvey Robert <mannl-KK0ffGbhmjU@public.gmane.org> 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..
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.
> 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 :)
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.00Btw: 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. -- Posted via http://www.ruby-forum.com/.
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
Am Samstag, den 19.11.2005, 18:05 +0100 schrieb Robert:> 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? >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. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
jmharvey.9956003-opCBI309nnGakBO8gow8eQ@public.gmane.org
2005-Nov-19 23:44 UTC
Re: Re: Localized Floats
I agree, floats aren''t suited for monetary values. There''s a Currency class in my plugin for that, meant to be used with the rails composed_of directive. The actual value is stored in the database as an integer. It''s true that overriding to_s can break code which relies on Numerics formatting a certain way. I don''t think this will be an issue for most rails apps, and overriding to_s makes things a lot easier. If it''s a problem in a specific app, one can always disable the override. --- rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org wrote: 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 andsome other things, even> though they are often misused for it. Most ofthe time it won''t matter,> but in some cases it will, therefore it''s betterto do it right in the> first place and use BigDecimal. >