razorasp.net-mvc-5intcurrencydata-formats

Converting int to currency format


guys. I would like to ask if there's anyway to make an int input to currency format? I have tried using

[DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = true)]
public int TotalAmount { get; set; }

but, it would show an empty

@Html.EditorFor

And if I used

[DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
public int TotalAmount { get; set; }

It would only show 0.00

What I would like is when I input 10000, it would automatically format to

Rp. 10.000

inside the @Html.EditorFor field.

Any suggestions?


Solution

  • Try this:

    1. "C" or "c" Currency Result:

      [DisplayFormat(DataFormatString = "{0:C}", ApplyFormatInEditMode = true)] public decimal TotalAmount { get; set; }

    OR

       [DisplayFormat(DataFormatString = "{0:C0}")]
        public decimal TotalAmount { get; set; }
    
    1. "N" or "n" Number Result:

      [DisplayFormat(DataFormatString = "{0:N}", ApplyFormatInEditMode = true)] public decimal TotalAmount { get; set; }

    2. In Entity Framework

    [DataType(DataType.Currency)] public decimal TotalAmount { get; set; }

    EditorTemplates Example:

    Create a editor template for currency (~/Views/Shared/EditorTemplates/Currency.cshtml):

    Currency.cshtml

    @Html.TextBox("", string.Format("{0:c}", ViewData.Model),new { @class = "text-box single-line" })
    

    Use:

    @Html.EditorFor(model => model.TotalAmount, "Currency")
    

    you can get more information form this and this link.