Reputation: 1122
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
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
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