⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decimaltochineseext.txt

📁 <数字转换汉字大写和英文读法的几个函数> 很早以前写的一个数字转换汉字大写和英文读法的几个函数,02年还修改过,可应用于delphi和Powerbuider,有兴趣的朋友可以改成其他
💻 TXT
字号:
/*{***********************数字转换汉字函数B定义***********************}
{介绍:参数number为string类型,Cntype(Integer类型)
 cntype=0:转换为人民币大写格式
 cntype=1:转化为数字大写格式
 例如:decimaltochineseExt('45092034.54',0)  :'肆仟伍佰零玖万贰仟零叁拾肆元伍角肆分
       decimaltochineseExt('45092034.54',1)  :'肆仟伍佰零玖万贰仟零叁拾肆点伍肆
 当转换为人民币大写时,number的小数位数不能超过2位!!}*/
/*注意:程序中调用了函数DecStrRound,该函数定义见DecStrRound.txt*/
string Numstr[12]//中文数位名
string str1,str2,intstr,decstr//整数部分,小数部分,大写整数部分,大写小数部分
string num[10]//各个数位段,即每四位为一段
string n[10]//各位数字
string number1,number2,number3//定义数字的三个数位段
int PointPos//小数点位置
int decpos
string Numhead//正负性
int Numlen//参数Number的串长度
string result						//函数返回值

if (Cntype<>0) and (CnType<>1) then 
	messageBox('参数错误','参数错误:选择转换类型参数值错误!',StopSign!)
	return('')
end if
if trim(Number)='' then 
	messageBox('参数','数字不能为空')
	return('')
end if
//初始化
numstr[1] ='零'
numstr[2] ='壹'
numstr[3] ='贰'
numstr[4] ='叁'
numstr[5] ='肆'
numstr[6] ='伍'
numstr[7] ='陆'
numstr[8] ='柒'
numstr[9] ='捌'
numstr[10]='玖'
numstr[11]='拾'
str1  =''
str2  =''
intstr=''
decstr=''
Result=''
//若第一位有”-“号,则取出
NumHead=''
if left(number,1)='-' then //可能是负数
 Number=mid(Number,2,len(number)-1)
 NumHead='负'
end if
//将形同'.'或者'.XXX'的数字加上整数位0
if Number='.' then 
	Number='0'
else 
	if mid(Number,1,1)='.' then 
		Number='0'+Number 
	end if
end if
Numlen=Len(number)//获得数字的长度
//先校验参数number是不是数字格式
//校验是否只有一个小数点
Decpos=0
for PointPos=1 to Numlen 
	if mid(Number,PointPos,1)='.' then Decpos=Decpos+1 
next
if Decpos>1 then 
	messageBox('参数错误','参数错误:不是数字格式!',StopSign!)
	return('')
end if
//校验是否含有非数字和'.'的字母存在
for pointPos=1 to Numlen  
 if mid(Number,pointpos,1)='.' then continue
 if asc(mid(Number,pointpos,1))<48 or asc(mid(Number,pointpos,1))>57 then
	Result='error'
	exit
 end if 
next
if Result='error' then 
	messageBox('参数错误','参数错误:不是数字格式!',StopSign!)
	return('')
end if
//初始化完毕
//如果转换为人民币大写,对输入数字四舍五入处理,保留2位小数
//在此调用了函数DecStrRound
if Cntype=0 then  Number=DecStrRound(Number,2,false)

pointpos=pos(Number,'.')//得到小数点的位置

//以下将得到整数部分Str1和小数部分Str2
if pointpos=0 then//输入值为纯整数
  str1=Number
  str2=''
else//输入值为浮点数
  str1=mid(Number,1,PointPos - 1)//取整数部分
  str2=mid(Number,PointPos+1,Numlen -PointPos)//取小数部分
  //消除小数后面多余的0
  do while mid(str2,len(str2),1)='0' 
    str2=mid(str2,1,len(str2) -1)
  loop  
end if
 //消除整数部分前面的多余的0
do while mid(str1,1,1)='0'
   str1=mid(str1,2,len(str1)-1)
loop
if str1='' then str1='0'

//******************************分析转换整数部分*********************************

//**分析整数部分在100000000以上的
if len(str1)>8 then
   //按每4位为一段拆分成三段,逐段分析
   num[1]=mid(str1,1,len(str1) -8)//取8位以上的那部分数段
   num[2]=mid(str1,Len(str1) -7,4)//取千万到万的4位
   num[3]=mid(str1,Len(str1) -3,4)//取千到个位的4位
   number1=DecimalToChineseExt(num[1],1)+'亿'//通过函数嵌套调用,得到亿上的数段格式,即若干亿

   if dec(Num[2])=0 then//如果千万到万4位为0   
     if dec(Num[3])=0 then//并且末尾4位为0
      Number2=''//没有内容
     else     
      if dec(Num[3])<1000 then
       Number2=''//如果第三段也是零XX百十个,则中间段的"零"去掉
      else
       Number2=numstr[1]//读零
   	end if
     end if   
   else
     if dec(num[2])>1000 then //中间4位大于1000
      Number2=DecimalToChineseExt(num[2],1)+'万'
     else
      Number2=numStr[1]+DecimalToChineseExt(num[2],1)+'万'//不足一千万,则读X亿零xx百、十、万
     end if
   end if
   if dec(Num[3])=0 then//末尾4位为0
    Number3=''
   else   
     if dec(num[3])>1000 then//末尾4位大于1000
      Number3=DecimalToChineseExt(num[3],1)
     else
      Number3=numstr[1]+DecimalToChineseExt(num[3],1)//不足一千,则读X万零XX百、十
	  end if
   end if
   intstr=number1+number2+number3
end if

//****分析整数部分在10000~99999999之间的}
if (Len(str1)>=5) and (Len(str1)<=8) then
   num[1]=mid(str1,1,Len(str1)-4)//取得第一段(千万位到万位)
   //为方便分析,若不足4位,用'0'补齐为4位
   if Len(num[1])=3 then  num[1]='0'+num[1]
   if Len(num[1])=2 then  num[1]='00'+num[1]
   if Len(num[1])=1 then  num[1]='000'+num[1]
   num[2]=mid(str1,Len(str1)-3,4)//取得第二段(千位到个位)
   number1=DecimalToChineseExt(num[1],1)+'万'
   if dec(num[2])=0 then
    number2=''
   else
     if dec(num[2])>1000 then //中间4位大于1000
      Number2=DecimalToChineseExt(num[2],1)
     else
      Number2=numStr[1]+DecimalToChineseExt(num[2],1)
	  end if
   end if
   intstr=number1+number2
end if

//****分析整数部分不到10000的}
if Len(str1)<5 then
   num[1]=str1
   //不足4位,用'0'补齐
   if Len(num[1])=3 then  num[1]='0'+num[1]
   if Len(num[1])=2 then  num[1]='00'+num[1]
   if Len(num[1])=1 then  num[1]='000'+num[1]
   number1=''//亿以上的为空
   number2=''//万以上的为空
   //分析千位
   if mid(num[1],1,1)='0' then
    n[1]=''
   else
    n[1]=numstr[dec(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[dec(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    
     if (mid(num[1],1,1)='0') and (mid(num[1],2,1)='0') and (mid(num[1],3,1)='1') then//如果百位为0且十位为1则不读出壹字
      n[3]='拾'
     else
      n[3]=numstr[dec(mid(num[1],3,1))+1] +'拾'
     end if
   end if
   //分析个位
   if mid(num[1],4,1)='0' then
    n[4]=''
   else
    n[4]=numstr[dec(mid(num[1],4,1))+1]
   end if
   //
   if mid(num[1],Len(num[1])-2,3)='000' then//当末尾有000时   
     n[2]=''
     n[3]=''
     n[4]=''
   end if
   if mid(num[1],Len(num[1])-1,2)='00' then//当末尾有00时
     n[3]=''
     n[4]=''
   end if
   if mid(num[1],Len(num[1]),1)='0' then  n[4]=''//当末尾有0时
   //数段合并
   number3=n[1]+n[2]+n[3]+n[4]
   //取得整数位值
   intstr=number1+number2+number3
end if

//****如果整数为零,转换为"零"
if str1='0' then intstr=numstr[1]

//**********************{整数转换完毕}*******************************

//**************************分析和转换小数部分*************************
if Len(str2)>0 then //如果小数数段不为空,则分析小数
  if cntype=0 then//一.如果转换为人民币表达式
    //不足2位,用零补足空位
    if Len(str2)=1 then  str2=str2+'0'
     if mid(str2,1,1)='0' then//角为0
     	if Intstr='零' then n[1]='' else n[1]='零'//如果元为0,则不读0角,否则读零若干分
     else
     	n[1]=numstr[dec(mid(str2,1,1))+1]+'角'
     end if
    if mid(str2,2,1)='0' then
     n[2]=''
    else
     n[2]=numstr[dec(mid(str2,2,1))+1]+'分'
    end if
    decstr=n[1]+n[2]  
  else //二.如果转换为数字表达式
    if cntype=1 then
		 decstr=''
	    for decpos=1 to Len(str2) 
	      decstr=decstr+numstr[dec(mid(str2,decpos,1))+1]	      
		 next
	 end if
  end if
end if
//{小数转换完毕}

//输出本函数的结果***********************}
if cntype=0 then//将数字字串转换为人民币的大写格式
  if str2='' then//如果为纯整数
   result=NumHead+intstr+'元整'
  else
    if intstr='零' then//如果整数为零,就只显示小数
     result=NumHead+decstr
    else
     result=NumHead+intstr+'元'+decstr
    end if
  end if
end if
if cntype=1 then//将数字字串转换为普通大写格式
  if str2='' then//如果为纯整数
   result=NumHead+intstr
  else
   result=NumHead+intstr+'点'+decstr
  end if
end if
return(result)
//<<<<<<<<<<<<转换完毕>>>>>>>>>>>>>>>>>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -