Reputation: 3294
I have a property on my model like this:
[DisplayFormat(DataFormatString = "{0:C2}")]
public decimal TotalPrice { get; set; }
When the TotalPrice
is for example 800,00
then the view shows $ 800,00
and if the TotalPrice
is 800,01
(or something like that), the view shows $ 800,01
and this is also correct but my client want's that if the TotalPrice
contains decimals, then the view must show ,00
instead of the decimals.
I can't format the number with [DisplayFormat(DataFormatString = "{0:C0}")]
because it will show $ 800
and i don't want that.
What's the right formatting for this case?
To make an example:
TotalPrice = 800
//Output: $ 800,00
TotalPrice = 800.23
//Output: $ 800,00
TotalPrice = 800.63
//Output: $ 801,00
Upvotes: 0
Views: 1732
Reputation: 312
[DisplayFormat(DataFormatString = "{0:C2}")]
public decimal TotalPrice {
get ()
{return Math.Round(this.TotalPrice);}
set;
}
or if you need to be able to get the decimals from totalprice then
[DisplayFormat(DataFormatString = "{0:C2}")]
public decimal TotalPrice { get; set; }
[DisplayFormat(DataFormatString = "{0:C2}")]
public readonly decimal TotalRoundedPrice
{
get ()
{return Math.Round(this.TotalPrice);}
}
Upvotes: 3
Reputation: 2982
Make a new string property in your model that outputs the correct value:
public string TotalPriceDisplay
{
get { return Math.Round(this.TotalPrice).ToString ("F"); }
}
Upvotes: 0