函数如下
1.中间一部分代码可以提取出来作为公共方法,我这里并没有提取,因为我是在代码中动态添加的
2.样式目前只做了:123转为一百二十三这类的
drop function if EXISTS zz_convert_number_chinese;
create FUNCTION zz_convert_number_chinese (numStr VARCHAR(100),zhfs int,zhys int) RETURNS VARCHAR(100)
DETERMINISTIC
BEGIN
-- zhfs 转换方式 zhys 转换样式
DECLARE res VARCHAR(100) DEFAULT ''; -- 结果
DECLARE leftNumStr VARCHAR(100) ; -- 左侧数值
DECLARE leftNum BIGINT; -- 左侧数值
DECLARE leftPart1 INT; -- 左侧数值部分 4位
DECLARE leftPart2 INT; -- 左侧数值部分 4位
DECLARE leftPart3 INT; -- 左侧数值部分 4位
DECLARE leftPart4 INT ; -- 左侧数值部分 4位 DEFAULT 0
DECLARE leftNumResStr VARCHAR(100) DEFAULT ''; -- 左侧数值结果
DECLARE rightNumStr VARCHAR(100); -- 右侧数值
DECLARE rightNumResStr VARCHAR(100) DEFAULT ''; -- 右侧数值结果
DECLARE zfStr VARCHAR(10) DEFAULT ''; -- 正数 负数
DECLARE lastIsZero INT; -- 最后数值是否为0
DECLARE unitNames VARCHAR(100) DEFAULT '十,百,千,万,亿'; -- 单位
DECLARE chineseNums VARCHAR(100) DEFAULT '零,一,二,三,四,五,六,七,八,九'; -- 中文数字
if locate('.',numStr)>0 THEN -- 包含点
set leftNumStr=SUBSTRING_INDEX(numStr,'.',1);
set rightNumStr=SUBSTRING_INDEX(numStr,'.',-1);
ELSE
set leftNumStr=numStr;
set rightNumStr='';
end if;
if left(leftNumStr,1)='-' then set zfStr='负';set leftNumStr=REPLACE(leftNumStr,'-',''); end if;
if LENGTH(leftNumStr)>16 then
RETURN numStr; -- 超出范围
end if;
set leftNum=cast(leftNumStr as signed);
if leftNum=0 then
set leftNumResStr='零';
else
set leftPart1=leftNum % 10000;
set leftNum=leftNum / 10000;
set leftPart2=leftNum % 10000;
set leftNum=leftNum / 10000;
set leftPart3=leftNum % 10000;
set leftNum=leftNum / 10000;
set leftPart4=leftNum % 10000;
set leftNum=leftNum / 10000;
if leftPart1>0 then
set lastIsZero=1;
set @i=0;
set @leftPart1=leftPart1;
while @leftPart1>0 DO
set @digit=@leftPart1%10;
if @digit=0 THEN
if lastIsZero=0 then
set leftNumResStr=concat('零',leftNumResStr);
end if;
set lastIsZero=1;
else
set @tempUnitName='';
if @i<>0 then
set @tempUnitName=SUBSTRING_INDEX(SUBSTRING_INDEX(unitNames,',',@i),',',-1);
end if;
set @tempNumberChinese=SUBSTRING_INDEX(SUBSTRING_INDEX(chineseNums,',',@digit+1),',',-1);
set leftNumResStr=concat(@tempNumberChinese,@tempUnitName,leftNumResStr);
set lastIsZero=0;
end if;
set @leftPart1=@leftPart1 DIV 10;
set @i=@i+1;
end while;
if leftPart1<1000 then
set leftNumResStr=concat('零',leftNumResStr);
end if;
end if;
if leftPart2>0 then
if leftPart1>0 and leftPart2%10=0 then
if leftNumResStr<>'' and left(leftNumResStr,1)<>'零' then
set leftNumResStr=concat('零',leftNumResStr);
end if;
end if;
set lastIsZero=1;
set @i=0;
set @leftPart2=leftPart2;
set @leftPart2Chinese='';
while @leftPart2>0 DO
set @digit=@leftPart2%10;
if @digit=0 THEN
if lastIsZero=0 then
set @leftPart2Chinese=concat('零',@leftPart2Chinese);
end if;
set lastIsZero=1;
else
set @tempUnitName='';
if @i<>0 then
set @tempUnitName=SUBSTRING_INDEX(SUBSTRING_INDEX(unitNames,',',@i),',',-1);
end if;
set @tempNumberChinese=SUBSTRING_INDEX(SUBSTRING_INDEX(chineseNums,',',@digit+1),',',-1);
set @leftPart2Chinese=concat(@tempNumberChinese,@tempUnitName,@leftPart2Chinese);
set lastIsZero=0;
end if;
set @leftPart2=@leftPart2 DIV 10;
set @i=@i+1;
end while;
set leftNumResStr=concat(@leftPart2Chinese,'万',leftNumResStr);
if leftPart2<1000 then
if leftNumResStr<>'' and left(leftNumResStr,1)<>'零' then
set leftNumResStr=concat('零',leftNumResStr);
end if;
end if;
else
if leftNumResStr<>'' and left(leftNumResStr,1)<>'零' then
set leftNumResStr=concat('零',leftNumResStr);
end if;
end if;
if leftPart3>0 then
if leftPart2>0 and leftPart3%10=0 then
if leftNumResStr<>'' and left(leftNumResStr,1)<>'零' then
set leftNumResStr=concat('零',leftNumResStr);
end if;
end if;
set lastIsZero=1;
set @i=0;
set @leftPart3=leftPart3;
set @leftPart3Chinese='';
while @leftPart3>0 DO
set @digit=@leftPart3%10;
if @digit=0 THEN
if lastIsZero=0 then
set @leftPart3Chinese=concat('零',@leftPart3Chinese);
end if;
set lastIsZero=1;
else
set @tempUnitName='';
if @i<>0 then
set @tempUnitName=SUBSTRING_INDEX(SUBSTRING_INDEX(unitNames,',',@i),',',-1);
end if;
set @tempNumberChinese=SUBSTRING_INDEX(SUBSTRING_INDEX(chineseNums,',',@digit+1),',',-1);
set @leftPart3Chinese=concat(@tempNumberChinese,@tempUnitName,@leftPart3Chinese);
set lastIsZero=0;
end if;
set @leftPart3=@leftPart3 DIV 10;
set @i=@i+1;
end while;
set leftNumResStr=concat(@leftPart3Chinese,'亿',leftNumResStr);
if leftPart3<1000 then
if leftNumResStr<>'' and left(leftNumResStr,1)<>'零' then
set leftNumResStr=concat('零',leftNumResStr);
end if;
end if;
else
if leftNumResStr<>'' and left(leftNumResStr,1)<>'零' then
set leftNumResStr=concat('零',leftNumResStr);
end if;
end if;
if leftPart4>0 then
if leftPart3=0 then
set leftNumResStr=concat('亿',leftNumResStr);
end if;
set lastIsZero=1;
set @i=0;
set @leftPart4=leftPart4;
set @leftPart4Chinese='';
while @leftPart4>0 DO
set @digit=@leftPart4%10;
if @digit=0 THEN
if lastIsZero=0 then
set @leftPart4Chinese=concat('零',@leftPart4Chinese);
end if;
set lastIsZero=1;
else
set @tempUnitName='';
if @i<>0 then
set @tempUnitName=SUBSTRING_INDEX(SUBSTRING_INDEX(unitNames,',',@i),',',-1);
end if;
set @tempNumberChinese=SUBSTRING_INDEX(SUBSTRING_INDEX(chineseNums,',',@digit+1),',',-1);
set @leftPart4Chinese=concat(@tempNumberChinese,@tempUnitName,@leftPart4Chinese);
set lastIsZero=0;
end if;
set @leftPart4=@leftPart4 DIV 10;
set @i=@i+1;
end while;
set leftNumResStr=concat(@leftPart4Chinese,'万',leftNumResStr);
end if;
if left(leftNumResStr,1)='零' then set leftNumResStr=SUBSTR(leftNumResStr,2); end if;
end if;
-- 整数部分结束 开始小数部分
if rightNumStr<>'' then
set @i=0;
set @rightLength=LENGTH(rightNumStr);
while @i<@rightLength DO
set @rightIndex=cast(SUBSTRING(rightNumStr,@i+1,1) as signed);
set @tempNumberChinese=SUBSTRING_INDEX(SUBSTRING_INDEX(chineseNums,',',@rightIndex+1),',',-1);
set rightNumResStr=concat(rightNumResStr,@tempNumberChinese);
set @i=@i+1;
end while;
set leftNumResStr=concat(leftNumResStr,'点',rightNumResStr);
end if;
RETURN leftNumResStr;
END;
结果如下