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如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

MySQL 衍生表(Derived Tables)的使用

《MySQL衍生表(DerivedTables)的使用》本文主要介绍了MySQL衍生表(DerivedTables)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学... 目录一、衍生表简介1.1 衍生表基本用法1.2 自定义列名1.3 衍生表的局限在SQL的查询语句select