2015-11-26 145 views
1

如何通过指定要使用的符号在VB.NET中格式化货币£或$。如何在VB.NET中格式化货币

我一直在使用formatcurrency,但是我找不到在数值前面改变符号的方法。

+0

NET应该使用当前文化的符号。你是否尝试将这个符号用于外国文化? – Plutonix

+0

我已经建立了cultureinfo,如何指定英镑? ,对于美国来说,它就是我们。 –

+0

如果答案适用于您,请点击对号,将其从未答复列表中移除。 – Plutonix

回答

3

使用传统的VB函数,如FormatCurrency是有限的,因为他们只知道当前的文化。 .ToString("C2")将使用符号和小数的当前文化。要指定一个不同的文化:

Dim decV As Decimal = 12.34D 

Console.WriteLine("In France: {0}", decV.ToString("C2", New CultureInfo("fr-FR"))) 
Console.WriteLine("For the Queen! {0}", decV.ToString("C2", New CultureInfo("en-GB"))) 
Console.WriteLine("When in Rome: {0}", decV.ToString("C2", New CultureInfo("it-IT"))) 
Console.WriteLine("If you are Hungary: {0}", decV.ToString("C2", New CultureInfo("hu-HU"))) 
Console.WriteLine("For the US of A: {0}", decV.ToString("C2", New CultureInfo("en-US"))) 

输出:

在法国:12,34€
女王! £12.34
在罗马当:€12,34
如果你是匈牙利:12,34英尺
对于美国:$ 12.34

Table of Language Culture Names, Codes


你也可以有将外币字符串转换为值的问题,因为CDec只知道如何使用本地文化。您可以使用Decimal.TryParse并指定传入文化:

' Croatian currency value 
Dim strUnkVal = decV.ToString("C2", New CultureInfo("hr-HR")) 
Dim myVal As Decimal 

' if the string contains a valid value for the specified culture 
' it will be in myVal 
If Decimal.TryParse(strUnkVal, 
        NumberStyles.Any, 
        New CultureInfo("hr-HR"), myVal) Then 
    Console.WriteLine("The round trip: {0}", myVal.ToString("C2")) 
End If