How can I format decimal property to currency?

前端 未结 8 1492
眼角桃花
眼角桃花 2020-12-02 21:50

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

How can I do this?

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 22:44

    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);
        }
    }  
    

提交回复
热议问题