|
楼主 |
发表于 2022-6-18 22:56
|
显示全部楼层
重新换了个思路编写了代码,你试试这个。
- Public Function CNToNum(sDBNum)
- '=============================
- '中文大小写转阿拉伯数字函数
- '中文大写人民币数字转阿拉伯数字
- 'by mzbao
- '=============================
- Dim i%, k%, p%, sChar$, lastChar$, Exp%
- Dim figureArr(1 To 15), expArr(1 To 15), s
-
- For i = 1 To 3
- sDBNum = Replace(sDBNum, Mid("整角分", i, 1), "")
- sDBNum = Replace(sDBNum, Mid("点元圆", i, 1), ".")
- sDBNum = Replace(sDBNum, Mid("○", i, 1), "零")
- Next i
-
- For i = 0 To 9
- sDBNum = Replace(sDBNum, Mid("〇一二三四五六七八九", i + 1, 1), i)
- sDBNum = Replace(sDBNum, Mid("零壹贰叁肆伍陆柒捌玖", i + 1, 1), i)
- Next
-
- If Len(sDBNum) = 0 Then s = 0: GoTo EndCal
-
- If Not IsNumeric(Left(sDBNum, 1)) Then
- sDBNum = "1" & sDBNum
- End If
-
- For i = 1 To Len(sDBNum)
- sChar = Mid(sDBNum, i, 1)
- If IsNumeric(sChar & "0") Then
- If i > 1 And IsNumeric(lastChar & "0") Then
- figureArr(k) = figureArr(k) & sChar
- Else
- k = k + 1
- figureArr(k) = sChar
- End If
- Else
- Exp = InStr("十百千万十百千亿十百千兆", sChar)
- If Exp = 0 Then Exp = InStr("拾佰仟萬", sChar)
- expArr(k) = expArr(k) + Exp
- End If
- lastChar = sChar
- Next
-
- '指数调整
- For i = k To 1 Step -1
- If expArr(i) > p Then p = Int(expArr(i) / 4) * 4
- If expArr(i) < p Then expArr(i) = expArr(i) + p
- Next
-
- If k > 1 Then '调整尾数的指数
- If expArr(k) = 0 And expArr(k - 1) > 1 Then
- If Left(figureArr(k), 1) > 0 Then
- expArr(k) = expArr(k - 1) - 1
- End If
- End If
- End If
-
- For i = 1 To k
- If expArr(i) > 0 And figureArr(i) = 0 Then figureArr(i) = 1
- s = s + figureArr(i) * 10 ^ expArr(i)
- Next
-
- s = Val(s)
- EndCal:
- CNToNum = s
- End Function
复制代码 |
|