I want to format a decimal value as a currency value.
How can I do this?
You can use String.Format, see the code [via How-to Geek]:
decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
// Output: $1,921.39
See also:
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);
}
}