2
我正在研究將貨幣值轉換爲單詞的項目。我發現一些代碼可以幫助我進行這種轉換(以及從前一個問題中實現一些代碼),並且代碼可以正常工作並執行我所需要的代碼......但是我得到的問題是Visual Basic不斷收集我的TEXT轉換。將數字轉換爲單詞時停止舍入
我本來有我的變量雙打,讀取小數趨於保留值並將所有移動到十進制,但它仍然轉換我的話一個。我還試圖在任何轉換仍然發生之前刪除小數點位置,但文本字仍然取整。例如,如果我在我的支票中輸入14,508.85,則文本轉換會吐出「14,000五百九和85/100」
單步執行程序時,它看起來像正在改變Modulus調用的值數百種轉換功能。 Mod是程序(類項目)的一個要求。有沒有我失蹤的事情? imgur.com/Nrym9qy
感謝您的幫助!
的代碼如下
Public Class Check
Dim checkValue As Decimal
Dim tempValue As Decimal
Dim result As String = ""
Dim temp As Decimal
Dim change As String = ""
Dim tempString As String = ""
Dim tempChange As Decimal
Private Function hundredsConvert(ByVal num As Decimal) As String
Static teens() As String = {"Zero", "One",
"Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Eleven", "Twelve",
"Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eightteen", "Nineteen"}
Static tens() As String = {"Twenty",
"Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"}
' If the number is 0, return an empty string.
If num = 0 Then Return ""
' Handle the hundreds digit.
Dim digit As Decimal
Dim result As String = ""
If num > 99 Then
digit = num \ 100
num = num Mod 100
result = teens(digit) & " Hundred"
End If
' If num = 0, we have hundreds only.
If num = 0 Then Return result.Trim()
' See if the rest is less than 20.
If num < 20 Then
' Look up the correct name.
result &= " " & teens(num)
Else
' Handle the tens digit.
digit = num \ 10
num = num Mod 10
result &= " " & tens(digit - 2)
' Handle the final digit.
If num > 0 Then
result &= " " & teens(num)
End If
End If
Return result.Trim()
End Function
Private Function numToString(ByVal num As Decimal) As String
'Get the change'
change = num.ToString
change = change.Substring(change.IndexOf(".") + 1) + "/100"
tempString = num.ToString
tempString.Remove(tempString.IndexOf(".") + 2)
num = Decimal.Parse(tempString)
Static groups() As String = {" ", "Thousand ", "Million",
"Billion", "Trillion", "Quadrillion", "?", "??",
"???", "????"}
Dim result As String = ""
'Process the groups, smallest first.'
Dim quotient As Decimal
Dim remainder As Decimal
Dim group_num As Decimal = 0
Do While num > 0
'Get the next group of three digits.'
quotient = num \ 1000
remainder = num Mod 1000
num = quotient
'Convert the group into words.'
result = hundredsConvert(remainder) &
" " & groups(group_num) &
result
'Get ready for the next group.'
group_num += 1
Loop
'Remove the trailing ", ".
If result.EndsWith(", ") Then
result = result.Substring(0, result.Length - 2)
End If
Return result
End Function
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
'Dim declarations'
'Convert check value from a text field to a double'
Try
checkValue = txtCheckAmount.Text
Catch ex As InvalidCastException
MessageBox.Show("You must enter a numbers to write a check.")
End Try
lblWrittenDollars.Text = numToString(checkValue) & "and " & change
If checkValue = 0 Then
MessageBox.Show("You must have some value in order to write a check.")
txtCheckAmount.Clear()
lblWrittenDollars.Text = ""
Else
Dim finalize = MessageBox.Show("You have specified that you want to write a check for " & checkValue & "?", "", MessageBoxButtons.YesNo)
If finalize = DialogResult.No Then
txtCheckAmount.Clear()
lblWrittenDollars.Text = ""
ElseIf finalize = DialogResult.Yes Then
End If
End If
checkValue = 0
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Private Sub txtPayOrderTo_TextChanged(sender As Object, e As EventArgs) Handles txtPayOrderTo.TextChanged
End Sub
End Class
你是否在numToString中對兩個ToString()調用設置了斷點?我最初的猜測是四捨五入發生在那裏。既然你把錢當作錢,你可以傳入一個格式字符串作爲參數,讓你保留你的小數位。 –
它看起來像在使用百分比轉換函數中的Modulus調用值正在改變。 Mod是程序(類項目)的一個要求。有沒有我失蹤的事情? http://imgur.com/Nrym9qy –
這可能是VB隱藏了你的四捨五入,但從我記得,mod應該使用十進制。這就是說,嘗試調用Math.Floor後傳入的數字。這將給你只是整數部分沒有舍入,只要你沒有負值的工作,在這種情況下,你不應該這樣做。如果你想使用負值,你需要考慮Math.Truncate。 –