在逗号后用两个零格式化数字?
Format number with two zeros after comma?
我的模型上有一个 属性 是这样的:
[DisplayFormat(DataFormatString = "{0:C2}")]
public decimal TotalPrice { get; set; }
例如,当 TotalPrice
是 800,00
时,视图显示 $ 800,00
,如果 TotalPrice
是 800,01
(或类似的东西),视图显示 $ 800,01
和 这也是正确的 但我的客户想要的是,如果 TotalPrice
包含小数,那么视图必须显示 ,00
而不是小数。
我无法使用 [DisplayFormat(DataFormatString = "{0:C0}")]
格式化数字,因为它会显示 $ 800
而我不想那样。
这种情况的正确格式是什么?
举个例子:
TotalPrice = 800
//Output: $ 800,00
TotalPrice = 800.23
//Output: $ 800,00
TotalPrice = 800.63
//Output: $ 801,00
在您的模型中创建一个输出正确值的新字符串 属性:
public string TotalPriceDisplay
{
get { return Math.Round(this.TotalPrice).ToString ("F"); }
}
[DisplayFormat(DataFormatString = "{0:C2}")]
public decimal TotalPrice {
get ()
{return Math.Round(this.TotalPrice);}
set;
}
或者如果您需要能够从总价中获取小数,那么
[DisplayFormat(DataFormatString = "{0:C2}")]
public decimal TotalPrice { get; set; }
[DisplayFormat(DataFormatString = "{0:C2}")]
public readonly decimal TotalRoundedPrice
{
get ()
{return Math.Round(this.TotalPrice);}
}
我的模型上有一个 属性 是这样的:
[DisplayFormat(DataFormatString = "{0:C2}")]
public decimal TotalPrice { get; set; }
例如,当 TotalPrice
是 800,00
时,视图显示 $ 800,00
,如果 TotalPrice
是 800,01
(或类似的东西),视图显示 $ 800,01
和 这也是正确的 但我的客户想要的是,如果 TotalPrice
包含小数,那么视图必须显示 ,00
而不是小数。
我无法使用 [DisplayFormat(DataFormatString = "{0:C0}")]
格式化数字,因为它会显示 $ 800
而我不想那样。
这种情况的正确格式是什么?
举个例子:
TotalPrice = 800
//Output: $ 800,00
TotalPrice = 800.23
//Output: $ 800,00
TotalPrice = 800.63
//Output: $ 801,00
在您的模型中创建一个输出正确值的新字符串 属性:
public string TotalPriceDisplay
{
get { return Math.Round(this.TotalPrice).ToString ("F"); }
}
[DisplayFormat(DataFormatString = "{0:C2}")]
public decimal TotalPrice {
get ()
{return Math.Round(this.TotalPrice);}
set;
}
或者如果您需要能够从总价中获取小数,那么
[DisplayFormat(DataFormatString = "{0:C2}")]
public decimal TotalPrice { get; set; }
[DisplayFormat(DataFormatString = "{0:C2}")]
public readonly decimal TotalRoundedPrice
{
get ()
{return Math.Round(this.TotalPrice);}
}