2

I have an ASP.NET MVC app. This app is using Razor in the views. I am trying to display a decimal?. The twist on this is that I do NOT want to show decimals. In other words, if the nullable decimal value is 567.89, I just want to display 567. Currently, I have a plain old:

<div class="icon">@Model.Count</div>

This approach displays 567.89. How do I format this nullable decimal to only show the whole number portion?

Thank you!

JQuery Mobile
  • 6,221
  • 24
  • 81
  • 134

1 Answers1

2

You can use String.Format to display the decimal? with out the decimals.

@String.Format("{0:0}", 567.89)

Will display 567

JayL
  • 251
  • 2
  • 5
  • What will this display if the value is NULL? – ganders Nov 03 '14 at 21:04
  • The value will be an empty string. – JayL Nov 03 '14 at 21:06
  • 1
    If you want to provide a default value for null, you can use the null-coalescing operator on the second param: `@string.Format("{0:0}", nullableDecimal ?? 0)`. – Chris Pratt Nov 03 '14 at 21:24
  • You can also consider applying the `DisplayFormat` attribute to your property (setting `DataFormatString` and `NullDisplayText` properties) and using `@DisplayFor(m => m.Count)` –  Nov 03 '14 at 22:18