views:

2225

answers:

3

I have an app that deals with currency. For display purposes I use the nifty VB FormatCurrency function which will format based on the OS's region setting. So, if in France you might get 123,45 where in the US you would get 123.45.

To perform calculation on these amounts I use CDec() to convert to decimal.

My problem is that when I convert the Decimal to a String using toString() it formats according to the currently set region. I need to be able to always convert the decimal into a String representation for the US, i.e. with decimal points.

I thought I would be able to do something similar to this: .toString("#0.00")

Thanks

A: 

Try passing InvariantCulture into your ToString() method:

Dim dec As Decimal = 1.25D
dec.ToString(System.Globalization.CultureInfo.InvariantCulture);
//writes 1.25
Rex M
+1  A: 

Try:

value.ToString("C", CultureInfo.InvariantCulture)

More info here:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

The "C" is to format for currency, but you can use lots of different format strings. If you really want it formatted for "US" rather than the invariant culture you can do this:

value.ToString("C", CultureInfo.CreateSpecificCulture("en-US"))
Joel Coehoorn
+1  A: 

This

    Dim dec As Decimal = 1.25D
    Dim s As String
    s = dec.ToString("C2", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))

produces $1.25

This

    s = dec.ToString("N2", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))

produces 1.25

dbasnett