Search code examples
c#formatcurrency

Format currency in ASP.NET 5 C#


@model IQueryable<Product>

@foreach (var p in Model )
{
        <div>
            <h3>@p.Name</h3>
            @p.Description
            <h4>@p.Price.ToString("C", new CultureInfo("us-US"))</h4>
        </div>
}

I would like to express the currency format on my own.

Like USD $ 285.00

I tried with CurrencyPositivePattern, but failed all.

In index.cshtml file, how can I do this?

thank you.


Solution

  • Try something like this:

    <h4>USD $ @p.Price.ToString("n2")</h4>
    

    This will output USD $ first, and then take the Price value and format it to 2 digits after the decimal point, numerically.

    See this official MS docs for more details on how to properly format numeric values, and all the options you have to do so.