mysql阿拉伯数字转换中文数字函数

2024-09-06 14:44

本文主要是介绍mysql阿拉伯数字转换中文数字函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

函数如下

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 0DECLARE leftNumResStr VARCHAR(100) DEFAULT ''; -- 左侧数值结果DECLARE rightNumStr VARCHAR(100); -- 右侧数值DECLARE rightNumResStr VARCHAR(100) DEFAULT ''; -- 右侧数值结果DECLARE zfStr VARCHAR(10) DEFAULT ''; -- 正数 负数DECLARE lastIsZero INT; -- 最后数值是否为0DECLARE 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);ELSEset 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 DOset @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 DOset @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 DOset @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 DOset @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 DOset @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;

结果如下

这篇关于mysql阿拉伯数字转换中文数字函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1142297

相关文章

MySQL主从同步延迟问题的全面解决方案

《MySQL主从同步延迟问题的全面解决方案》MySQL主从同步延迟是分布式数据库系统中的常见问题,会导致从库读取到过期数据,影响业务一致性,下面我将深入分析延迟原因并提供多层次的解决方案,需要的朋友可... 目录一、同步延迟原因深度分析1.1 主从复制原理回顾1.2 延迟产生的关键环节二、实时监控与诊断方案

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

MySQL数据库约束深入详解

《MySQL数据库约束深入详解》:本文主要介绍MySQL数据库约束,在MySQL数据库中,约束是用来限制进入表中的数据类型的一种技术,通过使用约束,可以确保数据的准确性、完整性和可靠性,需要的朋友... 目录一、数据库约束的概念二、约束类型三、NOT NULL 非空约束四、DEFAULT 默认值约束五、UN

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

MySQL 多表连接操作方法(INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL OUTER JOIN)

《MySQL多表连接操作方法(INNERJOIN、LEFTJOIN、RIGHTJOIN、FULLOUTERJOIN)》多表连接是一种将两个或多个表中的数据组合在一起的SQL操作,通过连接,... 目录一、 什么是多表连接?二、 mysql 支持的连接类型三、 多表连接的语法四、实战示例 数据准备五、连接的性

MySQL中的分组和多表连接详解

《MySQL中的分组和多表连接详解》:本文主要介绍MySQL中的分组和多表连接的相关操作,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录mysql中的分组和多表连接一、MySQL的分组(group javascriptby )二、多表连接(表连接会产生大量的数据垃圾)MySQL中的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指