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

📄 decimaltochinese.txt

📁 <数字转换汉字大写和英文读法的几个函数> 很早以前写的一个数字转换汉字大写和英文读法的几个函数,02年还修改过,可应用于delphi和Powerbuider,有兴趣的朋友可以改成其他
💻 TXT
字号:
//***********************数字转换汉字函数A定义***********************
/*介绍:参数number(Decimal类型),Cntype(integer类型)
 cntype=0:转换为人民币大写格式
 cntype=1:转化为数字大写格式
 例如:decimaltoChinese(45092034.541,0)  :'肆仟伍佰零玖万贰仟零叁拾肆元伍角肆分
       decimaltoChinese(45092034.541,1)  :'肆仟伍佰零玖万贰仟零叁拾肆点伍肆壹
 数字精度18位
 */
string numstr[12]             //中文数位名
string str1,str2,intstr,decstr//整数部分,小数部分,大写整数部分,大写小数部分
string num[10]                //各个数位段,即每四位为一段
string n[10]						//各位数字
string number1,number2,number3//定义数字的三个数位段
int pointpos						//小数点位置
int decpos
string NumHead						//正负性
string NumberName 				//用于代替参数Number的字符串
int NumLen 							//参数Number的串长度
string result						//函数返回值
//判断参数的正确性
if (Cntype<>0) and (CnType<>1) then 
	messageBox('参数错误','参数错误:选择转换类型参数值错误!',StopSign!);	
	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=''
//初始化完毕

if Number<0 then//如果是负数,则在前面加‘负'
  NumHead='负'
  Number =-Number
else
  NumHead=''
end if  
NumberName=string(Number)//转换为字符串
if NumberName='' then 
	messagebox('错误','输入数字不合法')
	return('')
end if

if Cntype=0 then
	//如果转换为人民币,小数只保留2位
   if (len(NumberName) - pos(NumberName,'.',1))>2 and (pos(NumberName,'.',1)>0) then
   	Number=Round(Number,2)
	end if
end if
NumberName=string(Number)		  //得到数字的字符形式
NumLen    =Len(NumberName)		  //得到数字的位数
pointpos  =pos(NumberName,'.',1)//得到小数点的位置

//分别得到数字的整数部分str1和小数部分str2
if pointpos=0 then//输入值为纯整数
  str1=NumberName
  str2=''
else              //输入值为浮点数    
  str1=Mid(numberName,1,PointPos - 1)              //取整数部分
  str2=Mid(numberName,PointPos+1,NumLen - PointPos)//取小数部分
  //小数部分去除尾0
  do while  mid(str2,len(str2),1)='0'
	str2=mid(str2,1,len(str2)-1)	
  loop
  if dec(str2)=0 then 							 //如果小数部分为0,则取消小数部分的分析
   str2=''
  end if
end if

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

//分析整数部分在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=DecimalToChinese(dec(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=DecimalToChinese(dec(num[2]),1)+'万'
     else
      Number2=numStr[1]+DecimalToChinese(dec(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=DecimalToChinese(dec(num[3]),1)
     else
      Number3=numstr[1]+DecimalToChinese(dec(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]
   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]=mid(str1,len(str1)-3,4);//取得第二段(千位到个位)
   number1=DecimalToChinese(dec(num[1]),1)+'万';
   if dec(num[2])=0 then
   	number2=''
   else
   	if dec(num[2])>1000 then //中间4位大于1000
      	Number2=DecimalToChinese(dec(num[2]),1)
	   else
   	   Number2=numStr[1]+DecimalToChinese(dec(num[2]),1)	//不足一千则读零XXX
		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]
	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]=''	//千位为0,读空
   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]=''	//千、百位为0,读空
     else
      n[2]=numstr[1]	//千位不是0,百位是0,读零
	  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]='' //百、十位为0,读空
     else
      n[3]=numstr[1] //百位不是0,十位是0,读零
	  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
      n[3]='拾' //如果百位为0且十位为1则不读出壹拾,而读拾
     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
   //做0位处理
   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//当末尾有0时
    n[4]=''
	end if
   //数段合并
   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  //一.如果转换为人民币表达式     
      if len(str2)=1 then  //不足2位,用零补足空位
			str2=str2+'0'
		end if
	   if mid(str2,1,1)='0' then//角为0
		  if Intstr='零' then //如果元为0,则不读0角,否则读零若干分     
			n[1]='' 
		  else 
			n[1]='零'
	     end if
	   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
    	for decpos=1 to len(str2) 
      	n[decpos]=numstr[dec(mid(str2,decpos,1))+1]
	      decstr=decstr+n[decpos]
		 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 + -