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

相关文章

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

浅谈mysql的not exists走不走索引

《浅谈mysql的notexists走不走索引》在MySQL中,​NOTEXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引,下面就来介绍一下mysql的notexists走不走索... 在mysql中,​NOT EXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引。以下

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU