Em 25-06-2010 11:22, Norman Clarke escreveu:
On Fri, Jun 25, 2010 at 10:31, Rodrigo Rosenfeld Rosas
<[email protected]> wrote:
What is the suggested path for dealing with number formats in Rails?
Even if we set locale to pt-BR, for instance, if the user inputs a number
like '1.000,00' or '1000,00' in a form input, numerical validation will
yield an 'invalid_number' error, even though this is a valid format in
Brazil.
We use the same formats in Argentina, but I luckily I haven't yet been
bitten by this. By sheer circumstance, I can't recall any times where
I had a field that could be either a float or an integer that wasn't
for money. So in my experience it's been easy to determine how to
interpret the commas and periods, and cast the param string to an
appropriate numeric value before passing it into the model.
But if you have a field that can admit arbitrary numeric values in an
app that needs to support multiple locales, then you could definitely
get bitten because 100,001 and 100.001 could mean different things to
different people. So perhaps it might make sense if you could add a
local-sensitive `I18n.to_f` and `.to_i`, as well as a validator.
Regards,
Norman
Hi Norman,
This is not about easy of interpreting numbers, but about consistency
and DRY.
In my case, this is also a currency situation and although it is clear
how to parse the string, we shouldn't be concerned about this while
defining our models.
This is something that a framework should do by its own since it is
feasible, DRY and less error prone and I can't see any reasons for not
supporting this on the Rails core side.
For instance, consider this pseudo-code snippet that could be put in the
numericality validator code:
def parse_raw_value_as_a_number(raw_value)
raw_value.gsub!(I18n.separator_char,
'').gsub!(I18n.delimiter_char, '.')
...
end
WDYT?
Best regards,
Rodrigo.
--
You received this message because you are subscribed to the Google Groups "Ruby on
Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.