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

相关文章

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp