我如何可以格式化小数属性货币 [英] How can i format decimal property to currency
本文介绍了我如何可以格式化小数属性货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要格式化,吸附剂中的值,并返回一个格式化货币值。
I want to format the value in the getter and return a formated currency value.
这是可能的,或者我是否需要财产申报作为一个字符串,然后使用的String.Format。
Is this possible or do i need to declare the property as a string and then use string.format.
推荐答案
属性可以返回他们想要什么,但它会需要返回正确的类型。
Properties can return anything they want to, but it's going to need to return the correct type.
private decimal _amount;
public string FormattedAmount
{
get { return string.Format("{0:C}", _amount); }
}
有人问...如果它是一个可空小数。
Question was asked... what if it was a nullable decimal.
private decimal? _amount;
public string FormattedAmount
{
get
{
return _amount == null ? "null" : string.Format("{0:C}", _amount.Value);
}
}
这篇关于我如何可以格式化小数属性货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文