3

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!

2

1 Answer 1

2

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

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

Will display 567

Sign up to request clarification or add additional context in comments.

4 Comments

What will this display if the value is NULL?
The value will be an empty string.
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).
You can also consider applying the DisplayFormat attribute to your property (setting DataFormatString and NullDisplayText properties) and using @DisplayFor(m => m.Count)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.