Thursday, September 20, 2007

Formatting Currency to Pence and Cents

Almost anyone will know how to format a decimal to a currency string. It's nice and easy to make a number like 1.25 format like £1.25, (or $1.25 if you're using a CultureInfo that has a dollar CurrencySymbol in its NumberFormatInfo.

But, say I have the decimal 0.06, and I want this to format to 6p. Or I might have 0.065 which I want to show as 6.5p.

How do I do that?

I understand I may need to multiply this decimal by 100 to get the whole number of pence, but how do I know which symbol to append? Plus, how do I know which end of the string the symbol needs to go?

There is nothing in a NumberFormatInfo that describes these things. The CurrencySymbol gives us our $, £, €, etc, but where's the pence and cents symbols?

Any ideas?

Right now we've got a workaround in place that works, but shouldn't this be a part of the NumberFormatInfo?

I've posted a question on the MSDN Forums about this.

UPDATE: Kevin posted a question on the MSDN Forums for this as well, and got a more satisfactory answer than I did.

Read that post here.

No comments: