如何在 VB.NET 中格式化货币

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33946347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:35:45  来源:igfitidea点击:

How to format currency in VB.NET

vb.net

提问by Simon J

How can I format the currency in VB.NET by specifying the symbol to use e.g. £ or $.

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

I have been using formatcurrency however I can't find a way to change the symbol in front of the value.

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

回答by ??ssa P?ngj?rdenlarp

Using the legacy VB functions like FormatCurrencyis limited because they only know the current culture. .ToString("C2")will use the current culture for the symbol and decimal. To specify a different culture:

使用像这样的遗留 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")))

Output:

输出:

In France: 12,34
For the Queen! £12.34
When in Rome: 12,34
If you are Hungary: 12,34 Ft
For the US of A: $12.34

在法国:12,34
为女王!£12.34
在罗马时:12,34
如果您是匈牙利:12,34 英尺
A 的美国:12.34 美元

Table of Language Culture Names, Codes

语言文化名称、代码表



You can also have problems converting a foreign currency string to a value because CDeconly knows how to use the local culture. You can use Decimal.TryParseand specify the incoming culture:

您也可能在将外币字符串转换为值时遇到问题,因为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