使用 VB.Net 将数字格式化为货币

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15709876/
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-09 16:47:20  来源:igfitidea点击:

Format number to currency using VB.Net

vb.netformattingcurrency

提问by Simon

I got for you today a simple problem that already took me part of the day with no real achievement. I'm trying to format a number to display as a currency. There's many ways to do that, I think I've done most of 'em.

我今天为您解决了一个简单的问题,该问题已经占用了我一天的时间,但没有取得真正的成就。我正在尝试格式化一个数字以显示为货币。有很多方法可以做到这一点,我想我已经完成了大部分。

So here's the problem: I'm adding multiple number which can be positive and negative, I can't know. When I'm formatting at the end using TextBox.Text = Format(variable, "C"), I got the right format for positive numbers (which is per example 123 456,00 $, I live in Canada) and I have (123 456,00 $)for negative ones. I would prefer to have the "-" symbol at the beginning.

所以这里的问题是:我正在添加可以是正数和负数的多个数字,我不知道。当我最后使用 格式化时TextBox.Text = Format(variable, "C"),我得到了正确的正数格式(每个例子123 456,00 $,我住在加拿大),我有(123 456,00 $)负数。我宁愿在开头使用“-”符号。

I searched the web to find other ways to do that, per example : FormatCurrency("-123 456", 2, TriState.True, TriState.False). That way I'm able to get rid of the parentheses but the "-" is AFTER the number (123 456,00 $-).

我在网上搜索以找到其他方法来做到这一点,每个例子: FormatCurrency("-123 456", 2, TriState.True, TriState.False)。这样我就可以去掉括号,但“-”在数字之后(123 456,00 $-)

Next one: SpecificCulture. so here I have NegativeValueHere.ToString("C", CultureInfo.CreateSpecificCulture("fr-CA")). With that I'm back at the beginning with my parentheses with (123 456,00 $). Note here that my application is running with culture set to ("fr-CA")since that's where I live.

下一张:SpecificCulture。所以在这里我有NegativeValueHere.ToString("C", CultureInfo.CreateSpecificCulture("fr-CA"))。有了这个,我又回到了开头的括号中(123 456,00 $)。请注意,我的应用程序在文化设置为("fr-CA")的情况下运行,因为那是我居住的地方。

I tried one more but can't remember what it was.However the "$" was in front of the number like $-123 456,00.

我又试了一个,但不记得是什么了。但是“$”在数字前面,比如$-123 456,00

Note: I'm running on VB.Net, and the number has to be formatted to be put in a readonly TextBox.

注意:我在 VB.Net 上运行,并且必须将数字格式化才能放入 readonly TextBox

采纳答案by nkvu

This seems to work for me:

这似乎对我有用:

Sub Main()
    Dim value As Decimal = -123.45
    Dim positiveValue As Decimal = 123.45
    Dim customCurrencyInfo As CultureInfo = CultureInfo.CreateSpecificCulture("fr-CA")

    customCurrencyInfo.NumberFormat.CurrencyNegativePattern = 8

    Dim formatString As String = value.ToString("C", customCurrencyInfo)
    Dim formatStringPositive As String = positiveValue.ToString("C", customCurrencyInfo)

    Console.WriteLine(formatString) '-123,45 $
    Console.WriteLine(formatStringPositive) '123,45 $
    Console.ReadLine()

End Sub

You can get the different pattern values for NumberFormat.CurrencyNegativePatternfrom thislink.

你可以得到不同的模式值NumberFormat.CurrencyNegativePattern这个链接。

Sorry if I'm off track

对不起,如果我跑偏了

回答by Hans Passant

You can create your own culture with a custom pattern for negative currency values. Take a look at the MSDN library article for NumberFormatInfo.CurrencyNegativePattern. For example:

您可以使用负货币价值的自定义模式创建自己的文化。查看 NumberFormatInfo.CurrencyNegativePattern 的 MSDN 库文章。例如:

Imports System.Globalization

Module Module1
    Sub Main()
        Dim simon = DirectCast(CultureInfo.GetCultureInfo("fr-CA").Clone, CultureInfo)
        simon.NumberFormat.CurrencyNegativePattern = 1
        Dim test = -1234.567
        Console.WriteLine(test.ToString("C", simon))
        Console.ReadLine()
    End Sub
End Module

Output: -$1?234,57

输出:-$1?234,57