Wafou Z
Wafou Z

Reputation: 85

How to properly display decimal?

I'm looking for a way to properly format and display a decimal value within my view.

For instance: 123456789,9876 should be 123 456 789,99.

Sometimes I want to display it like 123 millions.

Is there any way to have such rules when displaying decimal values into the View instead of having to write it like this?

<td align="right">
    @((item.Amount / 1000).ToString("### ###")) k&euro;
</td>
<td align="right">
    @((item.Ponderation * 100).ToString("f0")) &#37;
</td>

Upvotes: 0

Views: 1041

Answers (1)

stylishCoder
stylishCoder

Reputation: 385

Decorate your view model property with the [DisplayFormat] attribute and specify the desired format:

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

and then in your view:

@Html.DisplayFor(x => x.Testing)

Upvotes: 1

Related Questions