Format decimal to currency, should be in cents for values $0 to $1

Posted by Spongeboy on Stack Overflow See other posts from Stack Overflow or by Spongeboy
Published on 2011-01-13T04:24:14Z Indexed on 2011/01/13 6:53 UTC
Read the original article Hit count: 166

Filed under:
|
|
|

I have a decimal variable which represents a donation amount. Currently I am displaying it on screen as a currency like so-

DonationAmount.ToString("C");

This gives the following output (given a US locale)

1 -> $1.00
2 -> $2.00
0.5 -> $0.50

I am happy with the first two example, but want to have "0.5" show as "50c".

My current solution is with a conditional-

if (DonationAmount > 1)
   return (DonationAmount * 100m).ToString() + "c";
else
   return DonationAmount.ToString("C");

Is there a better way?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about formatting