vb.net 在VB.NET中将货币转换为十进制

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

Convert currency to decimal in VB.NET

vb.net

提问by user980492

I need to convert text in currency format to decimal format.

我需要将货币格式的文本转换为十进制格式。

Example: US$12,500.50 -> 12500.50

示例:12,500.50 美元 -> 12500.50

That text is into a textbox. I used the formatCurrency() function, but I need to get the original text (decimal format).

该文本进入文本框。我使用了 formatCurrency() 函数,但我需要获取原始文本(十进制格式)。

How can I do this?

我怎样才能做到这一点?

回答by Visual Vincent

I don't know for sure about this, but you can give it a try:

我不确定这一点,但你可以试一试:

Dim NewString As String = Replace(Replace(TextBox1.Text, "US$", ""), ",", "")
Dim Currency As Decimal = CDec(NewString)

'Just to check if it works.
MessageBox.Show(Currency)

回答by jmcilhinney

For a start, don't use FormatCurrency. Call ToStringand pass "c" as the format specifier. The inverse operation would be to call Decimal.Parseor Decimal.TryParseand specify currency formatting.

首先,不要使用FormatCurrency. 调用ToString并传递“c”作为格式说明符。相反的操作是调用Decimal.ParseDecimal.TryParse指定货币格式。

回答by dbasnett

Use NumberStyles

使用 NumberStyles

    Dim test As String = "US,500.50"
    Dim testAsNum As Decimal

    Dim ci As New Globalization.CultureInfo(Globalization.CultureInfo.CurrentCulture.LCID)
    ci.NumberFormat.CurrencySymbol = "US$"

    Dim ns As Globalization.NumberStyles = Globalization.NumberStyles.AllowCurrencySymbol Or _
        Globalization.NumberStyles.AllowDecimalPoint Or _
        Globalization.NumberStyles.AllowThousands

    If Decimal.TryParse(test, ns, ci, testAsNum) Then
        Stop
    Else
        Stop
    End If