惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
T
Threatpost
G
Google Developers Blog
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
H
Heimdal Security Blog
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
Hacker News: Ask HN
Hacker News: Ask HN
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Schneier on Security
B
Blog
V2EX - 技术
V2EX - 技术
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
C
Cybersecurity and Infrastructure Security Agency CISA
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Y
Y Combinator Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
N
News | PayPal Newsroom
Attack and Defense Labs
Attack and Defense Labs
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
S
Security @ Cisco Blogs
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
Project Zero
Project Zero
I
Intezer
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
SecWiki News
SecWiki News
Martin Fowler
Martin Fowler

博客园 - 会长

本地部署DeepSeek并用Python调用 Snowflake算法生成Id 用C语言实现ElGamal算法 如何用C#代码验证XML文件是否符合DTD规范 如何调试WebBrowser控件中的JS代码 解决Devexpress的RichEditControl控件保存为docx文件后在word里打开字体显示不正确的问题 《水浒传》中的物价 诗词记录 对开发流程优化的建议 如何在HP ProLiant-DL580 Gen9 上安装ESXi NumPy基础 闪存客户端 北京某软件园小公园 MySQL优化技巧 大叔学ML第五:逻辑回归 大叔学ML第四:线性回归正则化 大叔学ML第三:多项式回归 大叔学ML第二:线性回归 大叔学ML第一:梯度下降
一个用来将数字转换为英文的MySql函数
会长 · 2024-01-26 · via 博客园 - 会长

网上很容易找到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