Working with Currency (decimal)

Asked

Viewed 2,742 times

6

I’m working on a project where I had the need to use decimal for field Currency.

As the American standard and different from the Brazilian soon had some difficulties.

I found some examples of how to deal with it, but I was not very successful. Does anyone have any example or solution for this problem?

1 answer

5


In the Model, use like this:

    [Required]
    [DataType(DataType.Currency)]
    [Display(Name = "Price", ResourceType = typeof(Resources.Language))]
    public Decimal Price { get; set; }

In Views, use the Nuget Jquery Masked Input package, then you need to add a @section scripts in your View:

@section scripts {
    <script type="text/javascript">
        $(function() {
            $('#Price').maskMoney({ prefix: 'R$ ', allowNegative: false, thousands: '.', decimal: ',', affixesStay: false });
        });
    </script>
}

In fact, it is also good to inform the Web.config on the culture used. It is not exactly necessary for this case, but it can serve the rest of the system well.

<configuration>
  <system.web>
    ...
    <globalization uiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true" />
    ...
  </system.web>
</configuration>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.