123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- $PBExportHeader$f_decimaltocnf.srf
- global type f_decimaltocnf from function_object
- end type
- forward prototypes
- global function string f_decimaltocnf (decimal number, integer cntype)
- end prototypes
- global function string f_decimaltocnf (decimal number, integer cntype);/*将数字转换为汉字大写的人民币格式或数字格式。
- 如:输入 1999.23
- 转换为人民币:壹仟玖佰玖拾玖元贰角叁分
- 转换为数字为:壹仟玖佰玖拾玖点贰叁
- 用法:
- 输入数字类型变量:number
- 返回字符串: cnstr
- 参数整型变量:cntype: =0时,转换为人民币,=1时,转换为数字
- **<返回数字不大于18位精确值>
- cnstr=f_decimaltocn(number,cntype)
- */
- string numstr[11]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾"}
- string str1="",str2="",intstr="",decstr=""//整数部分,小数部分,大写整数部分,大写小数部分
- string num[]//各个数位段,即每四位为一段
- string n[]//各位数字
- string number1,number2,number3//定义数字的三个数位段
- int pointpos
- if cntype=0 then
- //如果转换为人民币大写,小数位数只取2位
- number=round(number,2)
- if isnull(number) then
- messagebox("错误","转换为人民币大写的数字整数位数最大17位!")
- return ''
- end if
- end if
- pointpos=pos(string(number),".")
- if pointpos=0 then//输入值为纯整数
- str1=string(number)
- str2=""
- else
- //输入值为浮点数
- str1=left(string(number),pointpos -1)//取整数部分
- str2=right(string(number),len(string(number)) -pointpos)//取小数部分
-
- end if
- if dec(str2)=0 then //如果小数部分为0,则取消小数部分的分析
- str2=""
- end if
- //*****分析转换整数部分*****
- choose case len(str1)
- case is>8//分析整数部分在100000000以上的
- num[1]=left(str1,len(str1) -8)//按每4位为一段拆分成三段,逐段分析
- num[2]=left(right(str1,8),4)
- num[3]=right(str1,4)
-
- number1=f_decimaltocn(dec(num[1]),1)+"亿"//若干亿
-
- if mid(num[2],1,1)="0" then//分析千万位
- n[1]=numstr[1]//若该位为0,则数位赋值“零”
- else
- n[1]=numstr[integer(mid(num[2],1,1))+1]+"仟"
- end if
- if mid(num[2],2,1)="0" then//分析百万位
- if mid(num[2],1,1)="0" then
- n[2]=""//若千万位为0,则该位为空
- else
- n[2]=numstr[1]
- end if
- else
- n[2]=numstr[integer(mid(num[2],2,1))+1]+"佰"
- end if
- if mid(num[2],3,1)="0" then//分析十万位
- if mid(num[2],2,1)="0" then
- n[3]=""
- else
- n[3]=numstr[1]
- end if
- else
- n[3]=numstr[integer(mid(num[2],3,1))+1] +"拾"
- end if
- if mid(num[2],4,1)="0" then//分析万位
- n[4]=""
- else
- n[4]=numstr[integer(mid(num[2],4,1))+1]
- end if
- //
- if right(num[2],3)="000" then//当末尾有000时
- n[2]=""
- n[3]=""
- n[4]=""
- end if
- if right(num[2],2)="00" then//当末尾有00时
- n[3]=""
- n[4]=""
- end if
- if right(num[2],1)="0" then//当末尾有0时
- n[4]=""
- end if
- //
- if mid(num[3],1,1)="0" then//分析千位
- n[5]=numstr[1]
- else
- n[5]=numstr[integer(mid(num[3],1,1))+1]+"仟"
- end if
- if mid(num[3],2,1)="0" then//分析百位
- if mid(num[3],1,1)="0" then
- n[6]=""
- else
- n[6]=numstr[1]
- end if
- else
- n[6]=numstr[integer(mid(num[3],2,1))+1]+"佰"
- end if
- if mid(num[3],3,1)="0" then//分析十位
- if mid(num[3],2,1)="0" then
- n[7]=""
- else
- n[7]=numstr[1]
- end if
- else
- n[7]=numstr[integer(mid(num[3],3,1))+1] +"拾"
- end if
- if mid(num[3],4,1)="0" then//分析个位
- n[8]=""
- else
- n[8]=numstr[integer(mid(num[3],4,1))+1]
- end if
- //
- if right(num[3],3)="000" then//当末尾有000时
- n[6]=""
- n[7]=""
- n[8]=""
- end if
- if right(num[3],2)="00" then//当末尾有00时
- n[7]=""
- n[8]=""
- end if
- if right(num[3],1)="0" then//当末尾有0时
- n[8]=""
- end if
- //
- //统计
- number2=n[1]+n[2]+n[3]+n[4]+"万"
- if num[3]="0000" then
- number3=""
- if num[2]="0000" then
- number2=""
- end if
- else
- if num[2]="0000" then
- number2=numstr[1]
- end if
- number3=n[5]+n[6]+n[7]+n[8]
- end if
- //取得整数位值
- intstr=number1+number2+number3
-
- case 5 to 8//分析整数部分在10000~99999999之间的
- num[1]=left(str1,len(str1) -4)//取得第一段(千万位到万位)
- //不足4位,用'0'补齐
- if len(num[1])=3 then
- num[1]="0"+num[1]
- end if
- if len(num[1])=2 then
- num[1]="00"+num[1]
- end if
- if len(num[1])=1 then
- num[1]="000"+num[1]
- end if
- num[2]=right(str1,4)//取得第二段(千位到个位)
- number1=""
- if mid(num[1],1,1)="0" then//分析千万位
- n[1]=""
- else
- n[1]=numstr[integer(mid(num[1],1,1))+1]+"仟"
- end if
- if mid(num[1],2,1)="0" then//分析百万位
- if mid(num[1],1,1)="0" then
- n[2]=""
- else
- n[2]=numstr[1]
- end if
- else
- n[2]=numstr[integer(mid(num[1],2,1))+1]+"佰"
- end if
- if mid(num[1],3,1)="0" then//分析十万位
- if mid(num[1],2,1)="0" then
- n[3]=""
- else
- n[3]=numstr[1]
- end if
- else
- n[3]=numstr[integer(mid(num[1],3,1))+1] +"拾"
- end if
- if mid(num[1],4,1)="0" then//分析万位
- n[4]=""
- else
- n[4]=numstr[integer(mid(num[1],4,1))+1]
- end if
- //
- if right(num[1],3)="000" then//当末尾有000时
- n[2]=""
- n[3]=""
- n[4]=""
- end if
- if right(num[1],2)="00" then//当末尾有00时
- n[3]=""
- n[4]=""
- end if
- if right(num[1],1)="0" then//当末尾有0时
- n[4]=""
- end if
- //
- if mid(num[2],1,1)="0" then//分析千位
- n[5]=numstr[1]
- else
- n[5]=numstr[integer(mid(num[2],1,1))+1]+"仟"
- end if
- if mid(num[2],2,1)="0" then//分析百位
- if mid(num[2],1,1)="0" then
- n[6]=""
- else
- n[6]=numstr[1]
- end if
- else
- n[6]=numstr[integer(mid(num[2],2,1))+1]+"佰"
- end if
- if mid(num[2],3,1)="0" then//分析十位
- if mid(num[2],2,1)="0" then
- n[7]=""
- else
- n[7]=numstr[1]
- end if
- else
- n[7]=numstr[integer(mid(num[2],3,1))+1] +"拾"
- end if
- if mid(num[2],4,1)="0" then//分析个位
- n[8]=""
- else
- n[8]=numstr[integer(mid(num[2],4,1))+1]
- end if
- //
- if right(num[2],3)="000" then//当末尾有000时
- n[6]=""
- n[7]=""
- n[8]=""
- end if
- if right(num[2],2)="00" then//当末尾有00时
- n[7]=""
- n[8]=""
- end if
- if right(num[2],1)="0" then//当末尾有0时
- n[8]=""
- end if
- //
- //统计
- if num[2]="0000" then
- number3=""
- else
- number3=n[5]+n[6]+n[7]+n[8]
- end if
- number2=n[1]+n[2]+n[3]+n[4]+"万"
- //取得整数位值
- intstr=number1+number2+number3
-
- case else//分析整数部分不到10000的
- num[1]=str1
- //不足4位,用'0'补齐
- if len(num[1])=3 then
- num[1]="0"+num[1]
- end if
- if len(num[1])=2 then
- num[1]="00"+num[1]
- end if
- if len(num[1])=1 then
- num[1]="000"+num[1]
- end if
- number1=""
- number2=""
- if mid(num[1],1,1)="0" then//分析千位
- n[1]=""
- else
- n[1]=numstr[integer(mid(num[1],1,1))+1]+"仟"
- end if
- if mid(num[1],2,1)="0" then//分析百位
- if mid(num[1],1,1)="0" then
- n[2]=""
- else
- n[2]=numstr[1]
- end if
- else
- n[2]=numstr[integer(mid(num[1],2,1))+1]+"佰"
- end if
- if mid(num[1],3,1)="0" then//分析十位
- if mid(num[1],2,1)="0" then
- n[3]=""
- else
- n[3]=numstr[1]
- end if
- else
- n[3]=numstr[integer(mid(num[1],3,1))+1] +"拾"
- end if
- if mid(num[1],4,1)="0" then//分析个位
- n[4]=""
- else
- n[4]=numstr[integer(mid(num[1],4,1))+1]
- end if
- //
- if right(num[1],3)="000" then//当末尾有000时
- n[2]=""
- n[3]=""
- n[4]=""
- end if
- if right(num[1],2)="00" then//当末尾有00时
- n[3]=""
- n[4]=""
- end if
- if right(num[1],1)="0" then//当末尾有0时
- n[4]=""
- end if
- //
- //统计
- number3=n[1]+n[2]+n[3]+n[4]
- ////取得整数位值
- intstr=number1+number2+number3
- end choose
- if str1="0" then//如果整数为零,转换为"零"
- intstr=numstr[1]
- end if
- //*****分析和转换小数部分*****
- if cntype=0 then//如果转换为人民币表达式
- // if len(str2)>2 then//如果小数位数大于2,就四舍五入.
- // str2=right(string(round(double("0"+"."+str2),2)),2)
- // end if
- if mid(str2,1,1)="0" then
- n[1]="零"
- else
- n[1]=numstr[integer(mid(str2,1,1))+1]+"角"
- end if
- if mid(str2,2,1)='0' then
- n[2]=""
- else
- n[2]=numstr[integer(mid(str2,2,1))+1]+"分"
- end if
- decstr=n[1]+n[2]
- end if
- if cntype=1 then//如果转换为数字表达式
- int decpos
- for decpos=1 to len(str2)
- n[decpos]=numstr[integer(mid(str2,decpos,1))+1]
- decstr=decstr+n[decpos]
- next
- end if
- //输出本函数的结果***********************
- if cntype=0 then//将数字字串转换为人民币的大写格式
- if str2="" then//如果为纯整数
- return intstr+"元整"
- else
- if intstr="零" then//如果整数为零,就只显示小数
- return decstr
- else
- return intstr+"元"+decstr+"整"
- end if
- end if
- end if
- if cntype=1 then//将数字字串转换为普通大写格式
- if str2="" then//如果为纯整数
- return intstr
- else
- return intstr+"点"+decstr
- end if
- end if
- //完毕OK
- end function
|