【问题标题】:How to convert currency to Decimal vb.net?如何将货币转换为十进制 vb.net?
【发布时间】:2016-05-16 13:20:00
【问题描述】:

我有远程数据,我必须显示两个项目的总成本。 这是我创建的一个示例,用于解决如何进行转换以添加这两个项目:

Dim test1 As String = "ZAR897.83"
Dim test2 As String = "ZAR900.83"
Dim t1 As Double = Val(test1)
Dim t2 As Double = Val(test2)
Dim TotalPrice As Decimal = 0.00
TotalPrice += CDec(t1)
TotalPrice += CDec(t2)

在上面的代码中 t1 结果值为 0.0 和 t2 相同。

任何添加这些值的帮助将不胜感激。

编辑:此问题已在此处提出但未得到解答:Convert currency to decimal in VB.NET

编辑:我没有展示我尝试过的所有不同的铸件,因为我尝试的所有铸件都产生了构建错误(我认为展示它们毫无意义,因为它们在某种意义上更不正确)。无论我如何尝试转换数字,我都会出错的原因是我的 Visual Studio 期望一个逗号而不是十进制值的句点,这是由于 Windows 中的区域设置设置不正确。

【问题讨论】:

  • 试试Decimal.Parse(t1, NumberStyles.Currency)
  • @VisualVincent "重载解析失败,因为无法使用这些参数调用可访问的 'Parse':" 它从那里继续,并带有额外的文本。
  • 如果“ZAR”应该是南非兰特,那么不,你不会从 Val() 或 Decimal.Parse() 中得到它。 .NET 将只接受“897.83R”。您可以先从字符串中删除 ZAR。
  • 这种行为不应该让人感到意外。如果您阅读文档 (msdn.microsoft.com/en-us/library/k7beh1x9(v=vs.90).aspx),它会明确指出“Val 函数在它无法识别为数字的一部分的第一个字符处停止读取字符串”
  • 这不是我的投票。我可以通过投票轻松证明这一点。在这里。

标签: vb.net currency


【解决方案1】:

也许你想要这样的东西

Imports System

Public Class Program
    Private Shared Function GetCurrencyValue(input As String) As Double
        Dim s As String = ""
        For i As Integer = 0 To input.Length - 1
            If Char.IsNumber(input(i)) OrElse input(i) = "."C OrElse input(i) = "-"C Then
                s += input(i)
            End If
        Next
        Return Double.Parse(s)
    End Function

Public Shared Sub Main()
    Dim test1 As String = "ZAR897.83"
    Dim test2 As String = "ZAR900.83"

    Dim d1 As Double = GetCurrencyValue(test1)
    Dim d2 As Double = GetCurrencyValue(test2)

    Dim TotalPrice As Decimal = 0.00D

    TotalPrice += CDec(d1)
    TotalPrice += CDec(d2)

    Console.WriteLine(TotalPrice)
End Sub
End Class

【讨论】:

  • 第 3 行中的“输入字符串格式不正确”。我还想确保这适用于任何货币,因此子字符串无论如何都是个人问题。
  • 谢谢,我使用了您的代码,该函数根据需要返回了数字。它也给出了一个错误,但我应该能够解决这个问题。
  • 您还需要检查函数中的"-" char
【解决方案2】:

我认为这会有所帮助

To Call this function use Dim ListOfPrices As String() = {"ZAR897.83", "ZAR900.83"} Dim ReturnedPrice = ReturnVal(ListOfPrices)

Function ReturnVal(ListOfPrices As String())
    Dim TotalPriceInStringFormat As String = "" 
    Dim myStringVal As String = ""
    Dim TotalPrice As Decimal = 0.0
    Dim MyBool As Boolean = False
    Dim count As Int16 = 0
    For Each priceInStrFormat As String In ListOfPrices
        TotalPriceInStringFormat = ""
        For Each c As Char In priceInStrFormat
            'If Char is Number or Period then append it
            If (Char.IsNumber(c) Or c = ".") Then
                TotalPriceInStringFormat += c
            ElseIf (Char.IsLetter(c) And MyBool = False) Then ' Extract ZAR from "ZAR897.83" string only once
                myStringVal += c
                count = count + 1
            End If
        Next
        If (count > 0) Then 'It already Extracted ZAR so assign MyBool to True
            MyBool = True
        End If
        TotalPrice += Convert.ToDecimal(TotalPriceInStringFormat.ToString())
    Next
    'If you want to return in this format ZAR900.83 Return TotalPriceWithString
    Dim TotalPriceWithString As String = myStringVal + TotalPrice.ToString
    'if you want return 900.83 then TotalPrice
    TotalPrice = Convert.ToDecimal(TotalPriceInStringFormat)

    Return TotalPrice
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多