首页 > 数据库 >一个用来将数字转换为英文的MySql函数

一个用来将数字转换为英文的MySql函数

时间:2024-01-26 19:23:17浏览次数:28  
标签:set 函数 -- when display tenEn 英文 MySql declare

网上很容易找到SQL Server等其它数据库转英文数字的函数,但是MySql我没有找到,故写了下来:


DELIMITER $$

CREATE FUNCTION ConvertThreeDigitInteger2EnWords(numStr char(3)) RETURNS varchar(50)
    DETERMINISTIC
BEGIN
	/*
    此函数接受一个形容'010'的用3位字符表示的数字,返回这个数字的英文表达。如果传入'000',返回空字符
    numStr:用字符表示的3位数字,左边不足需补0
    */
    declare numEnDisplay varchar(50) default ''; --  英文显示
    
    declare hundredEn varchar(50) default ''; -- 百位数
    declare tenEn varchar(50) default '';  -- 十位数
    declare numEn varchar(50) default ''; -- 个位数
    
    declare hundredNum char(1);  -- 百位数
    declare tenNum char(1);  -- 十位数
    declare num char(1); -- 个位数
  
    set hundredNum = substr(numStr, 1, 1);  
    set tenNum = substr(numStr, 2, 1); 
    set num = substr(numStr, 3, 1); 
    
    case hundredNum
        when '1' then set hundredEn = 'one hundred';
        when '2' then set hundredEn = 'two hundred';
        when '3' then set hundredEn = 'three hundred';
        when '4' then set hundredEn = 'four hundred';
        when '5' then set hundredEn = 'five hundred';
        when '6' then set hundredEn = 'six hundred';
        when '7' then set hundredEn = 'seven hundred';
        when '8' then set hundredEn = 'eight hundred';
        when '9' then set hundredEn = 'nine hundred';
        else set hundredEn = '';
    end case;
  
    if tenNum = '1' 
    then 
        case num
            when '1' then set tenEn = 'eleven';
            when '2' then set tenEn = 'twelve';
            when '3' then set tenEn = 'thirteen';
            when '4' then set tenEn = 'fourteen';
            when '5' then set tenEn = 'fifteen';
            when '6' then set tenEn = 'sixteen';
            when '7' then set tenEn = 'seventeen';
            when '8' then set tenEn = 'eighteen';
            when '9' then set tenEn = 'nineteen';
            else set tenEn = 'ten';
        end case;
    else 
        case tenNum
            when '2' then set tenEn = 'twenty';
            when '3' then set tenEn = 'thirty';
            when '4' then set tenEn = 'forty';
            when '5' then set tenEn = 'fifty';
            when '6' then set tenEn = 'sixty';
            when '7' then set tenEn = 'seventy';
            when '8' then set tenEn = 'eighty';
            when '9' then set tenEn = 'ninety';
            else set tenEn = '';
        end case;        
        case num
            when '1' then set numEn = 'one';
            when '2' then set numEn = 'two';
            when '3' then set numEn = 'three';
            when '4' then set numEn = 'four';
            when '5' then set numEn = 'five';
            when '6' then set numEn = 'six';
            when '7' then set numEn = 'seven';
            when '8' then set numEn = 'eight';
            when '9' then set numEn = 'nine';
            else set numEn = '';
        end case;
    end if;
    
    if hundredEn != '' then
        set numEnDisplay = hundredEn;
    end if;
    
    if hundredEn != '' and (tenEn != '' or numEn != '') then
        set numEnDisplay = CONCAT(numEnDisplay, ' and ');
    end if;
    
    if tenEn != '' then
        set numEnDisplay = CONCAT(numEnDisplay, tenEn);
    end if;
    
    if numEn != '' then
        if tenEn != '' then
            set numEnDisplay = CONCAT(numEnDisplay, '-', numEn);
        else
            set numEnDisplay = CONCAT(numEnDisplay, numEn);
        end if;
    end if;
    
    RETURN numEnDisplay;
END$$

DELIMITER ;
DELIMITER $$

CREATE FUNCTION ConvertNumber2EnWords(num decimal(14,2)) RETURNS varchar(200)
    DETERMINISTIC
BEGIN

   /*
   将一个数字转换为英文,最多能处理整数部分12位,小数部分为2位的数字。
    
   本函数遵循的规则:
   
   1,如果是0.00,返回 zero
   2,小数末尾0会被省略
   3,小数部分是00,末尾加 only
   4, 十位数前面会加 and,如 two hundred and forty
   */
   
    declare display varchar(200) default '';
    
    declare numStr varchar(15); -- 将数字转成字符串存于此
    declare pointIndex int; -- 小数点的位置
    declare numStrBeforePoint varchar(12); -- 整数部分
    declare numStrAfterPoint varchar(2); -- 小数部分
  
    declare billion char(3) default ''; -- billion 3 位
    declare million char(3) default ''; -- million 3 位
    declare thousand char(3) default ''; -- thousand 3 位
    declare low char(3) default ''; -- 最低 3 位
    
    declare billionEn varchar(50) default ''; -- billion 英文
    declare millionEn varchar(50) default ''; -- million 英文
    declare thousandEn varchar(50) default ''; -- thousand 英文
    declare lowEn varchar(50) default ''; -- 最低 3 位 英文    
    
    declare numCharAfterPointIndex int; -- 正在处理的小数位
    declare numCharAfterPoint char(1); -- 正在处理的小数

    if num = 0 then
        return 'zero'; -- 如果是0,提前返回
    end if;
    
    set numStr = cast(num as char);    
  
    set pointIndex = locate('.', numStr);
    
    if pointIndex = 0 then
        set numStrBeforePoint = numStr;
    else
        set numStrBeforePoint = substr(numStr, 1, pointIndex - 1);
    end if;
    
    set numStrBeforePoint = lpad(numStrBeforePoint, 12, '0'); -- 左边补0
    set numStrAfterPoint = trim(TRAILING '0' from substr(numStr, pointIndex + 1, 2));
    
    set billion = substr(numStrBeforePoint, 1, 3);
    set million = substr(numStrBeforePoint, 4, 3);
    set thousand = substr(numStrBeforePoint, 7, 3);
    set low = substr(numStrBeforePoint, 10, 3);
    
    set billionEn = ConvertThreeDigitInteger2EnWords(billion);
    set millionEn = ConvertThreeDigitInteger2EnWords(million);
    set thousandEn = ConvertThreeDigitInteger2EnWords(thousand);
    set lowEn = ConvertThreeDigitInteger2EnWords(low);
    
    if billionEn != '' then
        set display = concat(display, billionEn, ' billion');
    end if;
    
    if millionEn != '' then
        set display = concat(display, ' ', millionEn, ' million');
    end if;
    
    if thousandEn != '' then
        set display = concat(display, ' ', thousandEn, ' thousand');
    end if;    
   
    if low != '000' then
        if display = '' then
            set display = lowEn;
        else
            if substr(low, 1, 1) != '0' then
                set display = concat(display, ' ', lowEn);
            else
                set display = concat(display, ' and ', lowEn);
            end if;
        end if;
    end if;
    
    if numStrAfterPoint = '' then
        set display = concat(display, ' only');
    else
         -- 处理小数部分
         
        if display = '' then
            set display = 'zero'; -- 0.xx
        end if;
        
        set display = concat(display, ' point');
        set numCharAfterPointIndex = 1;
        while numCharAfterPointIndex <= length(numStrAfterPoint) do
            set numCharAfterPoint = substr(numStrAfterPoint, numCharAfterPointIndex, 1);
            case numCharAfterPoint
                when '1' then set display = concat(display, ' one');
                when '2' then set display = concat(display, ' two');
                when '3' then set display = concat(display, ' three');
                when '4' then set display = concat(display, ' four');
                when '5' then set display = concat(display, ' five');
                when '6' then set display = concat(display, ' six');
                when '7' then set display = concat(display, ' seven');
                when '8' then set display = concat(display, ' eight');
                when '9' then set display = concat(display, ' nine');
                else set display = concat(display, ' zero');
            end case;
            set numCharAfterPointIndex = numCharAfterPointIndex + 1;
        end while;
    end if;
   
RETURN display;
END$$

DELIMITER ;

image

标签:set,函数,--,when,display,tenEn,英文,MySql,declare
From: https://www.cnblogs.com/zzy0471/p/17990526

相关文章

  • MySQL数据库精选(从入门使用到底层结构)
    基本使用MySQL通用语法及分类DDL:数据定义语言,用来定义数据库对象(数据库、表、字段)DML:数据操作语言,用来对数据库表中的数据进行增删改DQL:数据查询语言,用来查询数据库中表的记录DCL:数据控制语言,用来创建数据库用户、控制数据库的控制权限DDL(数据定义语言)数据定义......
  • 2、mysql中的事务
    1.事务日志事务日志可以帮助提高事务的效率。使用事务日志,存储引擎在修改表的数据时只需要修改其内存拷贝,再把该修改行为记录到持久在硬盘上的事务日志中,而不用每次都将修改的数据本身持久到磁盘。事务日志采用的是追加的方式,因此写日志的操作是磁盘上一小块区域内的顺序IO......
  • The artifact mysql:mysql-connector-java:jar:8.0.33 has been relocated to com.mys
    Theartifactmysql:mysql-connector-java:jar:8.0.33hasbeenrelocatedtocom.mysql:mysql-connector-j:jar:8.0.33:MySQLConnector/Jartifactsmovedtoreverse-DNScompliantMaven2+coordinates.1.异常信息Theartifactmysql:mysql-connector-java:jar:8.0.33hasb......
  • pthread_detach函数
     线程分离状态:指定该状态,线程主动与主控线程断开关系。使用pthread_exit或者线程自动结束后,其退出状态不由其他线程获取,而直接自己自动释放。网络、多线程服务器常用。    进程若有该机制,将不会产生僵尸进程。僵尸进程的产生主要由于进程死后,大部分资源被释放,一点残留资......
  • 1、mysql概述
    1.连接管理与安全性每个客户端连接都会在服务器进程中拥有一个线程,这个连接的查询只会在这个单独的线程中执行,该线程只能轮流在某个CPU核心或者CPU中运行。服务器会负责缓存线程,因此不需要为每一个新建的连接创建或者销毁线程。当客户端(应用)连接到MySOL服务器时,服务器需要......
  • 【C++】 select函数介绍
    在使用C++语言开发网络应用程序时,常常需要用到select函数。select函数是一种多路复用机制,可以同时监听多个文件描述符上可读、可写、异常等事件,从而让程序能够高效地处理多个连接。下面详细介绍C++中的select函数。1.select函数的定义和作用select函数的定义为:intselect(intn......
  • 数据库安全||MySQL数据库安全中MySQL权限表:包括用户管理、权限管理等&用户管理:创建、
    MySQL数据库安全:MySQL数据库安全:MySQL权限表:包括用户管理、权限管理等。用户管理:创建、修改、删除用户账号。权限管理:查看、授予和撤销用户权限。1.MySQL数据库中的权限表是存储用户权限信息的核心部分,它们位于mysql数据库中。这些权限表包含了用户账号、密码、权限以及其他与用户......
  • Rust 中的函数式语言功能:迭代器与闭包
    对原文做了删减,原文参考Rust中的函数式语言功能:迭代器与闭包。目录闭包:可以捕获环境的匿名函数闭包会捕获其环境闭包类型推断和注解捕获引用或者移动所有权将被捕获的值移出闭包和Fntrait使用迭代器处理元素序列Iteratortrait和next方法消费迭代器的方法产生其他迭代器的......
  • 量化交易开发之函数API(四)
    量化交易开发之函数API(四)以前面策略代码为例进行讲解,如下:definitialize(context):run_daily(period,time='every_bar')g.security='000001.XSHE'defperiod(context):order(g.security,100)下面我们讲解一下python中的函数知识:order(......
  • 无涯教程-Scala - 函数声明
    函数是执行任务的一组语句,您可以将代码分成单独的函数,从逻辑上讲,划分通常是使每个函数执行特定任务。函数声明Scala函数声明具有以下形式-deffunctionName([listofparameters]):[returntype]如果您不使用等号和方法主体,则方法被隐式声明为抽象。函数定义Scala函数定......