[英]Format decimal property to currency inside class
I have created a class 我创建了一个班
public class XYZ: DomainObject
{
public decimal Price { get; set; }
public decimal Commission { get; set; }
}
Now I use Price
property in many place . 现在我在许多地方使用Price
属性。 I get a requirement to change my price value 5454
to 5,454.00
. 我要求将价格值5454
更改为5,454.00
。
So I use this one 所以我用这个
@String.Format("{0:N}", Model.Price)
But in this approach I have to do above thing at so many places I want to do something with my Price
property inside my class . 但是在这种方法中,我必须在很多地方做上面的事情,我想在类中使用Price
属性来做一些事情。 So that when ever a person try to get this property . 这样,任何时候有人试图获得此财产。 He gets the formatted value 5,454.00
. 他获得格式化值5,454.00
。
What should I do ? 我该怎么办 ?
Your property is a decimal
, so it has no format assigned to it. 您的属性是decimal
,因此没有分配格式。 You can add another property of string
and hide the format there: 您可以添加string
另一个属性,并在其中隐藏格式:
public class XYZ: DomainObject
{
public decimal Price { get; set; }
public string PriceString
{
get { return Price.ToString("N"); }
}
public decimal Commission { get; set; }
}
那么这样的事情呢:
public string PriceFormatted { get { return string.Format("{0:N}", Price); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.