如何将十进制属性格式化为货币? [英] How can I format decimal property to currency?

查看:60
本文介绍了如何将十进制属性格式化为货币?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将十进制值格式化为货币值.

I want to format a decimal value as a currency value.

我该怎么做?

推荐答案

属性可以返回他们想要的任何内容,但它需要返回正确的类型.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆