I would like to write a function that takes a currencyCode As String
input parameter and outputs a custom currencyFormatString
. My dilemma is deciding on the best (proper) way to do this. currencyCode
is an ISO 4217 Currency Code (e.g. "USD", "GBP", "JPY", etc.), and I want to perform the following steps:
- Check for null or empty strings.
- Convert the code to the corresponding currency symbol (e.g. "$", "£", "¥" as in the above).
- Format the returning string with the appropriate symbol.
I have two cases for the formatting: with cents, and without. In the case of JPY, there are never cents, so in this case I would always return the non-cents (not nonsense, :P) format.
My latest idea is to create a Dictionary
and populate it with the codes and symbols I need (FYI, we currently only need 6 codes/symbols and this very rarely changes). Is this a bad idea? Is there a better one? An XML file perhaps? I would love a code sample if you could. I'd provide one myself, but it's a scrambled mess of failed/less-than-ideal attempts at this point.
SOLUTIONS -- Based on hours of research, trial and error, and input from @tinstaafl.
'Method 1: Using Enumerable Type with Character Codes
' Limitation: Does not support currency symbols with more than one character.
Public Module SLCurrency
Enum ISOCurrency
USD = 36
CAD = 36
AUD = 36
EUR = 8364
GBP = 163
JPY = 165
End Enum
Public Function GetCurrencyFormat(ByVal currencyCode As String, ByVal withCents As Boolean) As String
Dim _numberFormatString, _currency As New ISOCurrency
[Enum].TryParse(Of ISOCurrency)(currencyCode, _currency)
If withCents AndAlso Not _curr.Equals(ISOCurrency.JPY) Then
_numberFormatString = "{0}* #,##0.00;[Red]{0}* (#,##0.00);{0}* ""-"";@"
Else
_numberFormatString = "{0}* #,##0;[Red]{0}* (#,##0);{0}* ""-"";@"
End If
Return String.Format(_numberFormatString, ChrW(_currency).ToString.Trim({ChrW(0)}))
End Function
End Module
'Method 2: Using a Dictionary of Currency Codes (key) and Symbols
' Advantage: Support currency symbols with more than one character,
' but is not quite as clean and can be more complex to implement.
Public Module SLCurrency
Dim CurrencyDict As New Dictionary(Of String, String) From {
{"USD", "$"}, {"CAD", "$"}, {"AUD", "$"}, {"EUR", "€"}, {"GBP", "£"}, {"JPY", "¥"}
}
Public Function GetCurrencyFormat(ByVal currencyCode As String, ByVal withCents As Boolean) As String
Dim _numberFormatString, _currency As String
_currency = String.Empty
CurrencyDict.TryGetValue(currencyCode, _currency)
If withCents AndAlso Not _currency.Equals("JPY") Then
_numberFormatString = "{0}* #,##0.00;[Red]{0}* (#,##0.00);{0}* ""-"";@"
Else
_numberFormatString = "{0}* #,##0;[Red]{0}* (#,##0);{0}* ""-"";@"
End If
Return String.Format(_numberFormatString, _currency)
End Function
End Module
I always welcome comments on my solutions. It helps me become a better programmer. :)
At this point, I think I'll go with Method 1 to keep the code a bit simpler as I do not anticipate a need for the added complexity to support longer currency symbols, especially since most of them are very obscure currencies which a business would be highly unlikely to ever accept as payment anyway.