0

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>
Wafou Z
  • 85
  • 10
  • Possible duplicate of [Format a decimal in a view](http://stackoverflow.com/questions/9721673/format-a-decimal-in-a-view) – NightOwl888 May 30 '16 at 11:44

1 Answers1

1

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)
stylishCoder
  • 385
  • 1
  • 19
  • In fact since i didnt understand the purpose of having a ViewModel i'm not using it. I mean in this case i have a User that has these attributes : int IdSap, string FirstName, string LastName, string Login, decimal? TotalRevenues and decimal? TotalSalesAssists And i only have to display the data... Is it relevant to use a ViewModel ? – Wafou Z May 30 '16 at 09:38
  • Best approach is to do with only decoration of display format attribute over model property..rest you can find any other way..i suggest you only the above way only .. i.e to create a Model class for User table – stylishCoder May 30 '16 at 09:42