金额转换大写
我有个文本框TXT1 和TXT2
如果TXT1文本框有数字金额的话 , 那TXT2文本框就要自动生成该数字的大写
比如TXT1里是20元 那 TXT2里就应该是贰拾元整
请问这个代码应该怎么写啊 --------------------编程问答-------------------- http://topic.csdn.net/u/20080125/09/04c65875-4f50-42cd-bd63-d7b1429e42e1.html --------------------编程问答-------------------- http://topic.csdn.net/u/20080125/16/e525de9d-e92f-44b9-8a8c-ea86a08f084b.html --------------------编程问答--------------------
--------------------编程问答-------------------- 自己写个小函数就可以解决啊!以后还可以重复使用,无非就是0---9吗. --------------------编程问答-------------------- MARK --------------------编程问答-------------------- 学点东西 --------------------编程问答--------------------
Private Const M_Tail As String = "整"
Private Const M_Zero As String = "零"
Private Const M_Cn As String = M_Zero & "壹贰叁肆伍陆柒捌玖"
Private Const M_Yuan As String = "元"
Private Const M_FomatStr As String = "万仟佰拾亿仟佰拾万仟佰拾" & M_Yuan & "角分"
Private Const M_Start As String = M_Zero & M_Yuan & M_Tail
Private Shared ReadOnly M_MaxLength As Integer = M_FomatStr.Length
Public Shared Function ConvertToCapsString(ByVal Money As Double) As String
Dim i, Max As Integer
Dim NZero As Integer = 0
Dim StrDec, StrPos, ch1 As String, ch2 As String = Nothing
Dim ChnNum As String = Nothing
Dim StrOnePosValue As String
Dim IntOnePosValue As Integer
Money = Math.Round(Math.Abs(Money), 2) '将num取绝对值并四舍五入取2位小数
StrDec = Convert.ToInt64(Money * 100).ToString '将num乘100并转换成字符串形式 (Convert:将一个基本类型转换成另一个基本类型)
Max = StrDec.Length '找出最高位
If Max > 15 Then
Return "数据过大"
Exit Function
ElseIf Money = 0 Then
ChnNum = M_Start '0的时候,直接输出“零元整”
Else
StrPos = M_FomatStr.Substring(M_MaxLength - Max) '从字符串的第一个位置开始检索
For i = 0 To Max - 1 '循环取出每一位需要转换的值
StrOnePosValue = StrDec.Substring(i, 1) '取出需转换的某一位的值
IntOnePosValue = Convert.ToInt32(StrOnePosValue) '转换为数字
If i <> Max - 3 AndAlso i <> Max - 7 AndAlso i <> Max - 15 Then '用andalso实现短路原则,提高程序执行效率
If StrOnePosValue = "0" Then '当所取位数不为元、万、亿、万亿上的数字时
ch1 = "" '此位为零,只计入零的个数。不转换。
ch2 = ""
NZero = NZero + 1
Else
If NZero = 0 Then '此位非零
ch1 = M_Cn.Substring(IntOnePosValue * 1, 1) '前面没有零,追加此位的中文
ch2 = StrPos.Substring(i, 1)
NZero = 0
Else
ch1 = M_Zero & M_Cn.Substring(IntOnePosValue * 1, 1) '前面已经有零,无论有多少只加一个零,然后追加此位的中文
ch2 = StrPos.Substring(i, 1)
NZero = 0
End If
End If
Else
If StrOnePosValue <> "0" AndAlso NZero <> 0 Then '该位是万亿,亿,万,元位等关键位
ch1 = M_Zero & M_Cn.Substring(IntOnePosValue * 1, 1)
ch2 = StrPos.Substring(i, 1)
NZero = 0
Else
If StrOnePosValue <> "0" AndAlso NZero = 0 Then
ch1 = M_Cn.Substring(IntOnePosValue * 1, 1)
ch2 = StrPos.Substring(i, 1)
NZero = 0
Else
If StrOnePosValue = "0" AndAlso NZero >= 3 Then
ch1 = ""
ch2 = ""
NZero = NZero + 1
Else
If Max > 11 Then
ch1 = "" '亿上
NZero = NZero + 1
Else
ch1 = "" '亿下
ch2 = StrPos.Substring(i, 1)
NZero = NZero + 1
End If
End If
End If
End If
End If
If i = Max - 11 OrElse i = Max - 3 Then
ch2 = StrPos.Substring(i, 1) '如果该位是亿位或元位,则必须写上
End If
ChnNum = ChnNum & ch1 & ch2
If i = Max - 1 AndAlso StrOnePosValue = "0" Then
ChnNum = ChnNum & M_Tail '最后一位(分)为0时,加上“整”
End If
Next
End If
Return ChnNum
End Function
Public Shared Function ConvertToCapsString(ByVal Money As String) As String
Dim num As Decimal = 0
Try
num = Convert.ToDecimal(Money)
Catch ex As Exception
Return "金额有误"
Exit Function
End Try
Return ConvertToCapsString(num)
End Function
-----------------
这段代码应该放哪里啊?
错误 1 未声明名称“ConvertToCapsString”。 C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 9 25 WindowsApplication1
我提示这样的错误啊! --------------------编程问答-------------------- 我放在模块里,引用是:
ConvertToCapsString(100.01) --------------------编程问答-------------------- 这个函数原先用vb6的时候写过,.net下没再用。
需要的话可以给我发私信 --------------------编程问答-------------------- 3楼的代码,调用一下~~
补充:.NET技术 , VB.NET