如何将货币转换为十进制 vb.net?
How to convert currency to Decimal vb.net?
我有远程数据,我必须显示两个项目的总成本。
这是我创建的示例,用于解决如何进行转换以添加这两项的问题:
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 中的区域设置设置不正确.
也许你想要这样的东西
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
我认为这会有所帮助
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
我有远程数据,我必须显示两个项目的总成本。 这是我创建的示例,用于解决如何进行转换以添加这两项的问题:
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 中的区域设置设置不正确.
也许你想要这样的东西
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
我认为这会有所帮助
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