Pro*_*ack 1 .net c# decimal xamarin.android xamarin
我有Article具有属性的模型SellPrice。我想在任何使用它的地方都用2小数点后的数字显示。2在小数点分隔符之后,它始终具有数字值,但是price例如2,30在显示为时2,3,我希望显示为2,30。对于Quantity同一Article模型中的属性,发生同样的事情,例如,如果1,1要将其值显示为,我希望在小数点分隔符后显示3个数字1,100。因为SellPrice我尝试了以下方法:
[Column("sell_price")]
[XmlElement(ElementName = "sell_price", Namespace = "http://tempuri.org/DataSet1.xsd")]
[DisplayFormat(DataFormatString = "{0:C}")]
public decimal SellPrice { get; set; }
但是DisplayFormat用红色下划线,并且不允许使用导入其名称空间System.ComponentModel.DataAnnotations。我猜它已经过时了。为了3在小数点分隔符后显示数字,我什至没有发现过时的东西。我发现了很多办法用它来办String.Format,但我使用SellPrice,并Quantity在大量的在我的项目的地方,我不想每次当我使用模型属性来写String.Format......有什么办法来指定例如在模型中作为属性?
为什么不使用私有字段将值保存在其中并具有两个属性SellPrice,SellPriceString如下所示,您可以重复使用该SellPriceString属性,而不必每次使用该属性时都格式化字符串SellPrice:
decimal _sellPrice;
public decimal SellPrice
{
    get
    {
        return _sellPrice;
    }
    set
    {
        _sellPrice = value;
    }
}
public string SellPriceString
{
    get
    {
        return _sellPrice.ToString("N2");
    }
}
在方法中使用标准数字格式作为参数ToString。您将对Quantity属性进行完全相同的操作,但是使用标准数字格式“ N3”,请再次参考链接以获取有关该格式的更多信息。