views:

128

answers:

5

I need to display data values in US currency format. Meaning 190.8 should display as $190.80. For some reason I cant figure out how to do this. Any advice?

+1  A: 

Standard Numeric Format Strings

   decimal moneyvalue = 1921.39m;
    string html = String.Format("Order Total: {0:C}", moneyvalue);
    Console.WriteLine(html);

or

double value = 12345.6789;
Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));//CultureInfo.GetCultureInfo("en-US")
// current culture is English (United States):
//       $12,345.68
Pranay Rana
+15  A: 

You could explicitly specify the US culture like so:

string.Format(CultureInfo.GetCultureInfo("en-US"), "{0:C}", decimalValue)

The C indicates the default currency format for the specified culture, in this case exactly what you're after. If you want the US currency symbol with a continental European number format (comma instead of period) then your job would be harder of course...

David M
A: 
String.Format("${0:n2}", 190.8m);
David
-1 for the wrong data type for a currency value.
David M
A: 
string usCurrency = (190.8m).ToString("c", CultureInfo.GetCultureInfo("en-US"));
lc
A: 
 decimal d = 190.8M;
 string displayData = d.ToString("c");

If your CurrentCulture is already US there's no need to explicitly supply it.

David Neale