vNext
vNext

Reputation: 1122

How to force asp .net MVC decimal displays without leading zero at the end (using DisplayFormat)

My value in database is 17.5 but when asp .net MVC render it (decimal), it always shows 17.50. How can I remove the zero at the end using DisplayFormat attribute (I mean -> 17.5)

Upvotes: 0

Views: 3669

Answers (2)

Shaiju T
Shaiju T

Reputation: 6609

Remove trailing zeros in view

View Model

[DisplayFormat(DataFormatString = "{0:G29}")]
public decimal? Discount { get; set; }

In View:

@Html.DisplayFor(model=> item.Discount)

Hope helps someone..

Upvotes: 1

Gabe
Gabe

Reputation: 50493

Use something like this:

0 - Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.

# - Replaces the "#" symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.

[DisplayFormat(DataFormatString = "{0:0.##}")]

Upvotes: 4

Related Questions