数字货币转换为大写格式-小周
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


数字货币转换为大写格式

发表时间:2009/10/28 9:14:38 评论(2) 浏览(13264)  评论 | 加入收藏 | 复制
   
摘 要:数字货币转换为大写格式
正 文:

数字货币转换为大写格式
以下为数字货币转换为大写格式程序, 首先建一个模块, 将以下程序复制进去并保存. (注: 最高位数为千万位)
调用方式为:
dollars = convertNum(inputValue)
  ^                        ^
须显示                  填写小
大写的                  写的控
控件                    件名

-------------------------------------------
Function GetChoice1(ByVal ind As Integer)
GetChoice1 = Choose(ind + 1, "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖")
End Function
Function GetChoice2(ByVal ind As Integer) '注意"byval",按值传递
Dim tempInt As Integer
ind = ind - 1
tempInt = ind \ 4 '取商
ind = ind Mod 4 '取余
GetChoice2 = IIf(ind > 0, Choose(ind, "拾", "佰", "仟", "万"), Choose(IIf(tempInt > 2, 1, tempInt), "万", "亿"))
End Function
'--------------------------------------------
'主函数convertNum
Function ConvertNum(Baval Num As Variant) As String
Dim i As Integer, j As Integer
Dim tempInt As Integer
Dim tempStr, ResultStr As String
tempStr = CStr(Num) '转换成字符型
j = Len(tempStr) '取得长度
For i = 1 To j '对每个数字进行大写转换
tempInt = CInt(Mid(tempStr, j - i + 1, 1)) '
ResultStr = GetChoice1(tempInt) & GetChoice2(i) & ResultStr
Next i
ConvertNum = ResultStr


以下为数字货币转换为大写格式程序, 首先建一个模块, 将以下程序复制进去并保存. (注: 最高位数为千万位)
调用方式为:
dollars = chMoney(Val(inputValue))
  ^                        ^
须显示                  填写小
大写的                  写的控
控件                    件名


' 名称: CCh
'        得到一位数字 N1 的汉字大写
'        0 返回 ""
Public Function CCh(N1) As String
Select Case N1
  Case 0
    CCh = "零"
  Case 1
    CCh = "壹"
  Case 2
    CCh = "贰"
  Case 3
    CCh = "叁"
  Case 4
    CCh = "肆"
  Case 5
    CCh = "伍"
  Case 6
    CCh = "陆"
  Case 7
    CCh = "柒"
  Case 8
    CCh = "捌"
  Case 9
    CCh = "玖"
End Select
End Function

'名称: ChMoney
'       得到数字 N1 的汉字大写。最大为 千万位。 O 返回
Public Function chMoney(N1) As String
Dim tMoney As String
Dim lMoney As String
Dim tn '小数位置
Dim s1 As String '临时STRING 小数部分
Dim s2 As String '1000 以内
Dim s3 As String '10000
Dim st1, t1

If N1 = 0 Then
  chMoney = " "
  Exit Function
End If
If N1 < 0 Then
  chMoney = "负" + chMoney(Abs(N1))
  Exit Function
End If
tMoney = Trim(Str(N1))
tn = InStr(tMoney, ".")  '小数位置
s1 = ""

If tn <> 0 Then
  st1 = Right(tMoney, Len(tMoney) - tn)
  If st1 <> "" Then
    t1 = Left(st1, 1)
    st1 = Right(st1, Len(st1) - 1)
    If t1 <> "0" Then
      s1 = s1 + CCh(Val(t1)) + "角"
    End If
    If st1 <> "" Then
     t1 = Left(st1, 1)
     s1 = s1 + CCh(Val(t1)) + "分"
    End If
  End If
  st1 = Left(tMoney, tn - 1)
Else
  st1 = tMoney
End If

s2 = ""
If st1 <> "" Then
  t1 = Right(st1, 1)
  st1 = Left(st1, Len(st1) - 1)
  s2 = CCh(Val(t1)) + s2
End If

If st1 <> "" Then
  t1 = Right(st1, 1)
  st1 = Left(st1, Len(st1) - 1)
  If t1 <> "0" Then
    s2 = CCh(Val(t1)) + "拾" + s2
  Else
    If Left(s2, 1) <> "零" Then s2 = "零" + s2
  End If
End If

If st1 <> "" Then
  t1 = Right(st1, 1)
  st1 = Left(st1, Len(st1) - 1)
  If t1 <> "0" Then
    s2 = CCh(Val(t1)) + "佰" + s2
  Else
    If Left(s2, 1) <> "零" Then s2 = "零" + s2
  End If
End If

If st1 <> "" Then
  t1 = Right(st1, 1)
  st1 = Left(st1, Len(st1) - 1)
  If t1 <> "0" Then
  s2 = CCh(Val(t1)) + "仟" + s2
  Else
    If Left(s2, 1) <> "零" Then s2 = "零" + s2
  End If
End If

s3 = ""
If st1 <> "" Then
  t1 = Right(st1, 1)
  st1 = Left(st1, Len(st1) - 1)
  s3 = CCh(Val(t1)) + s3
End If


If st1 <> "" Then
  t1 = Right(st1, 1)
  st1 = Left(st1, Len(st1) - 1)
  If t1 <> "0" Then
  s3 = CCh(Val(t1)) + "拾" + s3
  Else
    If Left(s3, 1) <> "零" Then s3 = "零" + s3
  End If
End If

If st1 <> "" Then
  t1 = Right(st1, 1)
  st1 = Left(st1, Len(st1) - 1)
  If t1 <> "0" Then
  s3 = CCh(Val(t1)) + "佰" + s3
  Else
   If Left(s3, 1) <> "零" Then s3 = "零" + s3
  End If
End If

If st1 <> "" Then
  t1 = Right(st1, 1)
  st1 = Left(st1, Len(st1) - 1)
  If t1 <> "0" Then
  s3 = CCh(Val(t1)) + "仟" + s3
  End If
End If
If Right(s2, 1) = "零" Then s2 = Left(s2, Len(s2) - 1)
If Len(s3) > 0 Then
  If Right(s3, 1) = "零" Then s3 = Left(s3, Len(s3) - 1)
  s3 = s3 & "万"
End If

chMoney = IIf(s3 & s2 = "", s1, s3 & s2 & "元" & s1)

End Function
如何加入换行符
C="A" + vbNewLine + "B"
[联系电话] = "1111" + Chr(13) + Chr(10) + "2222"
给一绑定文本框赋值,可以成功的看到换行效果:
1111
2222

相关:金额大小写示例


Access软件网交流QQ群(群号:198465573)
 
 相关文章
如何在在掩码中设置只允许输入大写格式的字母  【钱玉炜  2008/3/25】
【Access示例】十进制数与二进制数的互相转换  【缪炜  2013/3/15】
excel中,怎样才能把数字转换成中文大写金额格式  【赵文斌  2013/3/20】
时间格式的转换问题  【在水一方  2013/5/11】
【Access教程】使用access数据库时可能用到的数据转换  【漏蛧尐魚℡  2013/6/13】
【Access示例】利用交叉表实现行列的转换,使列变成yyyymm...  【缪炜  2013/7/5】
常见问答
技术分类
相关资源
文章搜索
关于作者

小周

文章分类

文章存档

友情链接