Answered FormatCurrency Question

ChrisMayhew

Member
Joined
Apr 2, 2009
Messages
12
Programming Experience
1-3
Hey guys/ladies :)

I am making a application that I need to use $ for. When I use FormatCurrency it puts it into £ and I assume this is because of my location that is set on my computer.

Is it possible to make it so that FormatCurrency uses $ regardless of the computers location?

Thanks,
Chris
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,393
Location
Sydney, Australia
Programming Experience
10+
Don't use FormatCurrency regardless. To convert a number to a string, call its ToString method. You can specify any standard or custom format string you like. For currency that would be:
VB.NET:
Dim currency As String = myNumber.ToString("c")
MSDN has all the information you need on format strings.

Now, that code, as is, will do the same as FormatCurrency, i.e. it will use the default currency format as specified in the regional settings of the current system. You can use another overload of ToString that takes a second argument of type IFormatProvider. The actual object you use will be a NumberFormat with all its CurrencyX properties set appropriately. You can either create a NumberFormat object yourself and set its properties manually, or you can create a CultureInfo object for a culture that uses the desired currency format by default, e.g. en-US, and then get its NumberFormat property.

As an alternative, you could just hard code the currency symbol, e.g.
VB.NET:
"$" & myNumber.ToString("n2")
 

ChrisMayhew

Member
Joined
Apr 2, 2009
Messages
12
Programming Experience
1-3
Don't use FormatCurrency regardless. To convert a number to a string, call its ToString method. You can specify any standard or custom format string you like. For currency that would be:
VB.NET:
Dim currency As String = myNumber.ToString("c")
MSDN has all the information you need on format strings.

Now, that code, as is, will do the same as FormatCurrency, i.e. it will use the default currency format as specified in the regional settings of the current system. You can use another overload of ToString that takes a second argument of type IFormatProvider. The actual object you use will be a NumberFormat with all its CurrencyX properties set appropriately. You can either create a NumberFormat object yourself and set its properties manually, or you can create a CultureInfo object for a culture that uses the desired currency format by default, e.g. en-US, and then get its NumberFormat property.

As an alternative, you could just hard code the currency symbol, e.g.
VB.NET:
"$" & myNumber.ToString("n2")

Thanks, I think i'll use the hard coded method for the symbol.
 
Top Bottom