用VB把数字转成中文字符串[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 thinkeasy 的 blog

根据数字的读法,写了一个把数字转成中文字符串的程序
参数一为数字
参数二为是不是反回人民币大写
参数三为是不是直接读数字,否则带有十百等单位
参数四为设置小数点后面的位数,默认为4
使用方法是
t=getchinanum(20005.000436, , , 7)'返回 “二千零五点零零零四三六”
t=getchinanum(2005.436, true, , 7)'返回“贰仟零伍元肆角肆分”
t=getchinanum(2005.436, , true, 7)'返加“二零零五点四三六”

下面是程序代码

function getchinanum(othernum as double, optional isrmb as boolean, optional numoption as boolean, optional dotnum as integer) as string
    on error resume next
    num = trim(str(int(othernum)))
   
    if isrmb then
        numwei = "拾佰仟万拾佰仟亿拾佰仟"
        numshu = "零壹贰叁肆伍陆柒捌玖拾"
    else
        numwei = "十百千万十百千亿十百千"
        numshu = "零一二三四五六七八九十"
    end if
    if othernum < 20 and othernum >= 10 then
        num = right(num, 1)
        getchinanum = left(numwei, 1)
    end if
    for i = 1 to len(num)
        bstr = mid(num, i, 1)
        if numoption then
            getchinanum = getchinanum + mid(numshu, val(bstr) + 1, 1)
           
        else
            getchinanum = getchinanum + mid(numshu, val(bstr) + 1, 1)
            if bstr = "0" then
                if mid(numwei, len(num) - i, 1) = "万" or mid(numwei, len(num) - i, 1) = "亿" then
                    do while right(getchinanum, 1) = "零"
                        getchinanum = left(getchinanum, len(getchinanum) - 1)
                    loop
                    getchinanum = getchinanum + mid(numwei, len(num) - i, 1)
                end if
               
            else

                getchinanum = getchinanum + mid(numwei, len(num) - i, 1)
            end if
            getchinanum = replace(getchinanum, "零零", "零")
        end if
    next i
    if numoption = false then
        do while right(getchinanum, 1) = "零"
            getchinanum = left(getchinanum, len(getchinanum) - 1)
        loop
    end if
    if isrmb then
        numrmb = "元角分"
        getchinanum = getchinanum + mid(numrmb, 1, 1)
        if val(num) <> othernum then
            num = trim(str(round(othernum - val(num), 2)))
            for i = 2 to len(num)
                bstr = mid(num, i, 1)
                getchinanum = getchinanum + mid(numshu, val(bstr) + 1, 1) + mid(numrmb, i, 1)
            next i
        else
            getchinanum = getchinanum + "整"
        end if
    else
        if val(num) <> othernum then
            if dotnum = 0 then dotnum = 4

本文关键:数字转成中文字符串
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top