Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow dot and comma in numbers, not only for decimal

I'm having a problem with localization. In Brazil, we use comma as a decimal separator, instead of dot. For example:

500,00
120,21
0,0001

I found the solution to this problem based on this answer: MVC 3 jQuery Validation/globalizing of number/decimal field

But here in Brazil, we also use "." in numbers, like:

100.000.000,00
11.125,23

And one more thing:

10.000 <> 10,000

The first one is ten thousand, and the second is simply ten.

Using the globalization plugin, when the user types the ".", it shows an validation error. I tried using the data annotation DisplayFormat, but it didn't work as expected... To "solve" this problems I'm using javascript to manually set and remove the "." from the numbers on the field, but this is very problematic when we need to change anything (and I'm sure this is one of the worst approaches I could use...). Do you guys have any idea of how to proceed in this case?

One more question: I can create a model binder (or modify the existing one) to accept this number format?

like image 212
Armando K. Avatar asked Oct 15 '13 18:10

Armando K.


People also ask

Who uses commas instead of decimals?

The three most spoken international auxiliary languages, Ido, Esperanto, and Interlingua, all use the comma as the decimal separator.

Why do some countries use commas instead of decimals?

Leibniz was an influential mathematician, and the dot as a multiplication sign became widespread in Europe. But this solution created another problem: The dot as a multiplication sign could be confused with the decimal point. So, European mathematicians started to use a comma to separate decimals.

Is it comma or dot in numbers?

Great Britain and the United States are two of the few places in the world that use a period to indicate the decimal place. Many other countries use a comma instead. The decimal separator is also called the radix character.


1 Answers

I just found this answer.

Fixing binding to decimals

It worked perfectly in my scenario. This guy solved the exactly same problem I was having!

I just had modify a few lines of code, but the most important part was this line:

ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());

I modified it to accept nullable values.

like image 195
Armando K. Avatar answered Oct 19 '22 02:10

Armando K.