How can I format decimal property to currency?

WingMan20-10 picture WingMan20-10 · Aug 13, 2010 · Viewed 109.9k times · Source

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

How can I do this?

Answer

Robaticus picture Robaticus · Aug 13, 2010

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