Question

I have an application 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 whereas in the US you would get 123.45.

To perform the 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")

Was it helpful?

Solution

Try passing InvariantCulture into your ToString() method:

Dim dec As Decimal = 1.25D
dec.ToString(System.Globalization.CultureInfo.InvariantCulture);
//writes 1.25

OTHER TIPS

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"))

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top