我编写了以下模块,用于将ISO货币代码解析为相应的符号:
Public Module ISOCurrency
Enum CurrencyChar
USD = 36 ' $
EUR = 8364 ' €
GBP = 163 ' £
JPY = 165 ' ¥
BRL = 82 ' R$ - 82 gets the 'R', but not the '$'
End Enum
Public Function GetCurrencyFormat(ByVal customerCurrencyCode As String, _
ByVal withCents As Boolean) As String
Dim _numberFormatString As String, _currency As New CurrencyChar
'_currency = 0 (i.e. " "c) if parse fails
[Enum].TryParse(Of CurrencyChar)(customerCurrencyCode, _currency)
'These strings are derived from Excel custom number format and made to be
' compatible with SpreadsheetLight
If withCents AndAlso Not _currency.Equals(CurrencyChar.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
然后我可以这样称呼它(例如用硬编码的货币):
Dim formatCode As String = ISOCurrency.GetCurrencyFormat("BRL", False)
我的问题是,这仅限于单字符货币符号,有些货币有多个字符;与巴西雷亚尔一样。
要求:我不一定需要使用枚举,但我希望解决方案能够引用如上所示的货币:即CurrencyChar.JPY
,并且也是可解析的来自传入的ISO代码字符串。
编辑:我没有任何本地化的经验,所以这是另一种我愿意研究的可能性,如果在这种情况下这是更好的做法。
答案 0 :(得分:1)
我会建议使用字典而不是枚举
Dim currencyInfo = new Dictionary(Of String, String) From {{"USD", "$"}, {"EUR", "€"}}
为了更进一步,您可以在类中添加格式。
Dim currencyInfo = new Dictionary(Of String, CurrencyInfo)
Class CurrencyInfo
Public Property Symbol As String
Public Property Format As String
End Class
如果您对特定货币而不是语言/国家/地区的货币感兴趣,则可以通过指定Language Culture Names进行文化工作。
Dim culture As System.Globalization.CultureInfo
culture = New System.Globalization.CultureInfo("en-US", False)
Console.WriteLine(culture.NumberFormat.CurrencySymbol)
culture = New System.Globalization.CultureInfo("ja-JP", False)
Console.WriteLine(culture.NumberFormat.CurrencySymbol)
culture = New System.Globalization.CultureInfo("pt-BR", False)
Console.WriteLine(culture.NumberFormat.CurrencySymbol)
答案 1 :(得分:0)
您可以使用属性执行此操作。 我添加扩展方法以更自然地公开它们。
现有的System.ComponentModel.DescriptionAttribute运行良好。
您也可以使用更多/更多属性创建自己的属性。
Imports System.Runtime.CompilerServices ' for Extension
Imports System.ComponentModel ' for Description Atttribute
Public Module ModTest
Public Enum Names As Integer
<Description("Bob Smith")>
Bob_Smith
<Description("Darren M B")>
Darren_MB
End Enum
Public Sub test()
Dim x As Names = Names.Darren_MB
Dim displayName As String = x.Description
End Sub
''' <summary>
''' Provides the Description Name for an enumeration based on the System.ComponentModel.Description Attribute on the enumeration value. if not found it returnes the defaultvalue passed.
''' Allows the description property of an enumeration to be exposed easily.
''' </summary>
''' <param name="EnumConstant">The Enumeration Item extended by the function.</param>
''' <param name="DefaultValue">Value returned when Description is not present.</param>
<Extension()> _
Public Function Description(ByVal EnumConstant As [Enum], ByVal DefaultValue As String) As String
Dim fi As Reflection.FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
Dim aattr() As DescriptionAttribute = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
If aattr.Length = 0 OrElse aattr(0).Description = "" Then
Return DefaultValue
Else
Return aattr(0).Description
End If
End Function
''' <summary>
''' Provides the Description Name for an enumeration based on the System.ComponentModel.Description Attribute on the enumeration value. if not found it returnes the Name of the Enum Item formatted (removes "_")
''' Allows the description property of an enumeration to be exposed easily.
''' </summary>
''' <param name="EnumConstant">The Enumeration Item extended by the function.</param>
<Extension()> _
Public Function Description(ByVal EnumConstant As [Enum]) As String
Return Description(EnumConstant, Replace(EnumConstant.ToString(), "_", " "))
End Function
end Module