Allow dot and comma in numbers, not only for decimal

15,166

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.

Share:
15,166
Armando K.
Author by

Armando K.

Updated on June 15, 2022

Comments

  • Armando K.
    Armando K. about 2 years

    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?