Integer to string formatted as currency

Since it has been a while since the last post, I thought I would give a quick trick that shows the power of string.Format().

Say you got an integer “price” that you want to display in a string, but formatted as currency. You can do this very easily using string.Format():

int price = 2795000;
string priceStr = string.Format("The price is {0:C}", price);

 

priceStr would now be “The price is 2.795.000,00 kr” if the Culture was set to Swedish. If you want to “lock” the culture do like this:

int price = 2795000;
string priceStr = string.Format(new CultureInfo("en-US"), "The price is {0:C}", price);

 

That would format it as $ instead, giving priceStr the value “$2,795,000.00″

I also found a great list of (all?) the possible ways to format using string.Format() that I can highly recommend here: http://blog.stevex.net/string-formatting-in-csharp/

No Comments Yet

You can be the first to comment!

Sorry, comments for this entry are closed at this time.