1. <i id='kOOSk'><tr id='kOOSk'><dt id='kOOSk'><q id='kOOSk'><span id='kOOSk'><b id='kOOSk'><form id='kOOSk'><ins id='kOOSk'></ins><ul id='kOOSk'></ul><sub id='kOOSk'></sub></form><legend id='kOOSk'></legend><bdo id='kOOSk'><pre id='kOOSk'><center id='kOOSk'></center></pre></bdo></b><th id='kOOSk'></th></span></q></dt></tr></i><div id='kOOSk'><tfoot id='kOOSk'></tfoot><dl id='kOOSk'><fieldset id='kOOSk'></fieldset></dl></div>
  2. <tfoot id='kOOSk'></tfoot>

    1. <small id='kOOSk'></small><noframes id='kOOSk'>

      <legend id='kOOSk'><style id='kOOSk'><dir id='kOOSk'><q id='kOOSk'></q></dir></style></legend>
        <bdo id='kOOSk'></bdo><ul id='kOOSk'></ul>

      MySQL 自定义函数将罗马数字转为阿拉伯数字

      时间:2023-08-21
      <legend id='V3gy3'><style id='V3gy3'><dir id='V3gy3'><q id='V3gy3'></q></dir></style></legend>

        <tbody id='V3gy3'></tbody>
    2. <tfoot id='V3gy3'></tfoot>
          <i id='V3gy3'><tr id='V3gy3'><dt id='V3gy3'><q id='V3gy3'><span id='V3gy3'><b id='V3gy3'><form id='V3gy3'><ins id='V3gy3'></ins><ul id='V3gy3'></ul><sub id='V3gy3'></sub></form><legend id='V3gy3'></legend><bdo id='V3gy3'><pre id='V3gy3'><center id='V3gy3'></center></pre></bdo></b><th id='V3gy3'></th></span></q></dt></tr></i><div id='V3gy3'><tfoot id='V3gy3'></tfoot><dl id='V3gy3'><fieldset id='V3gy3'></fieldset></dl></div>
            <bdo id='V3gy3'></bdo><ul id='V3gy3'></ul>

            <small id='V3gy3'></small><noframes id='V3gy3'>

              • 本文介绍了MySQL 自定义函数将罗马数字转为阿拉伯数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                好的,我需要一个 MySQL 函数来转换罗马数字字符串:

                例如XXCVI

                转换成它的阿拉伯数字等价物.至于我为什么需要它,这是一个很长的故事,我只是需要.

                基于某人发布的 PHP 函数,我创建了以下 MySQL 函数,但它似乎无休止地运行,我不知道为什么.(我可能只是太累了)

                有人对我的函数有什么问题有任何提示,或者有更有效的方法将罗马数字字符串转换为阿拉伯数字吗?

                DROP 函数如果存在`romeToArabic`$$CREATE DEFINER=`root`@`localhost` FUNCTION `romeToArabic`(roman_in VARCHAR(64)) 返回 int(11)开始声明数字 VARCHAR(2);声明 int_val INT;声明罗马 VARCHAR(64);DECLARE res INT;声明 no_more_rows 布尔值;声明 num_rows INT DEFAULT 0;DECLARE roman_cur CURSOR FOR SELECT num, val FROM roman_numeral ORDER BY id;DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;设置罗马 = UPPER(roman_in);设置资源= 0;如果存在 roman_numeral,则删除临时表;创建临时表 roman_numeral (`id` INT(8) NOT NULL AUTO_INCREMENT,`num` varchar(2) 默认为空,`val` int(8) NOT NULL, PRIMARY KEY (id)) ENGINE=MyISAM;INSERT INTO roman_numeral (num, val) VALUES ('M', 1000), ('CM', 900), ('D', 500), ('CD', 400), ('C', 100), ('XC', 90), ('L', 50), ('XL', 40), ('X', 10), ('IX', 9), ('V', 5), ('IV', 4), ('我', 1);打开 roman_cur;SELECT FOUND_ROWS() INTO num_rows;the_loop:环形FETCH roman_cur INTO 数字,int_val;IF no_more_rows THEN CLOSE roman_cur;离开 the_loop;万一;WHILE INSTR(罗马,数字)= 1 DOSET res = res + int_val;设置罗马= SUBSTRING(罗马,长度(数字));结束时;结束循环 the_loop;IF res>0 那么返回资源;别的返回-1;万一;完$$

                解决方案

                不知道为什么你不工作,但谷歌搜索很快,我想出了这个链接:

                http://forge.mysql.com/tools/tool.php?id=107

                CREATE FUNCTION fromRoman (inRoman varchar(15)) RETURNS integer DETERMINISTIC开始DECLARE 数字 CHAR(7) DEFAULT 'IVXLCDM';声明数字 TINYINT;DECLARE 前一个 INT DEFAULT 0;声明当前的INT;DECLARE sum INT DEFAULT 0;SET inRoman = UPPER(inRoman);虽然长度(罗马)>0 做SET digit := LOCATE(RIGHT(inRoman, 1), numeric) - 1;SET current := POW(10, FLOOR(digit/2)) * POW(5, MOD(digit, 2));SET sum := sum + POW(-1, current 

                Ok, I need a MySQL Function that will convert a Roman Numeral String:

                e.g. XXCVI

                Into its Arabic numbering equivalent. Its a long story as to why I need it, I just do.

                Based on a PHP function that someone posted, I created the following MySQL Function, but it seems to be running endlessly and I'm not sure why. (I might just be too tired)

                Anybody have any hints as to what's wrong with my function, or have a more efficient way to convert a roman numeral string into an Arabic number?

                DROP FUNCTION IF EXISTS `romeToArabic`$$
                CREATE DEFINER=`root`@`localhost` FUNCTION `romeToArabic`(roman_in VARCHAR(64)) RETURNS int(11)
                BEGIN
                  DECLARE numeral VARCHAR(2);
                  DECLARE int_val INT;
                  DECLARE roman VARCHAR(64);
                  DECLARE res INT;
                  DECLARE no_more_rows BOOLEAN;
                  DECLARE num_rows INT DEFAULT 0;
                  DECLARE roman_cur CURSOR FOR SELECT num, val FROM roman_numeral ORDER BY id;
                  DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
                  SET roman = UPPER(roman_in);
                  SET res = 0;
                
                  DROP TEMPORARY TABLE IF EXISTS roman_numeral;
                  CREATE TEMPORARY TABLE roman_numeral (
                      `id` INT(8) NOT NULL AUTO_INCREMENT,
                      `num` varchar(2) DEFAULT NULL,
                      `val` int(8) NOT NULL, PRIMARY KEY (id)) ENGINE=MyISAM;
                
                  INSERT INTO roman_numeral (num, val) VALUES ('M', 1000), ('CM', 900), ('D', 500), ('CD', 400), ('C', 100), ('XC', 90), ('L', 50), ('XL', 40), ('X', 10), ('IX', 9), ('V', 5), ('IV', 4), ('I', 1);
                
                  OPEN roman_cur;
                  SELECT FOUND_ROWS() INTO num_rows;
                
                 the_loop:
                  LOOP
                    FETCH  roman_cur INTO   numeral, int_val;
                    IF no_more_rows THEN CLOSE roman_cur;
                      LEAVE the_loop;
                    END IF;
                
                    WHILE INSTR(roman, numeral) = 1 DO
                      SET res = res + int_val;
                      SET roman = SUBSTRING(roman, LENGTH(numeral));
                    END WHILE;
                
                  END LOOP the_loop;
                  IF res > 0 THEN
                    RETURN res;
                  ELSE
                    RETURN -1;
                  END IF;
                END$$
                

                解决方案

                Not sure why your isnt working but googling was fast and I came up with this link:

                http://forge.mysql.com/tools/tool.php?id=107

                CREATE FUNCTION fromRoman (inRoman varchar(15)) RETURNS integer DETERMINISTIC
                BEGIN
                
                    DECLARE numeral CHAR(7) DEFAULT 'IVXLCDM';
                
                    DECLARE digit TINYINT;
                    DECLARE previous INT DEFAULT 0;
                    DECLARE current INT;
                    DECLARE sum INT DEFAULT 0;
                
                    SET inRoman = UPPER(inRoman);
                
                    WHILE LENGTH(inRoman) > 0 DO
                        SET digit := LOCATE(RIGHT(inRoman, 1), numeral) - 1;
                        SET current := POW(10, FLOOR(digit / 2)) * POW(5, MOD(digit, 2));
                        SET sum := sum + POW(-1, current < previous) * current;
                        SET previous := current;
                        SET inRoman = LEFT(inRoman, LENGTH(inRoman) - 1);
                    END WHILE;
                
                    RETURN sum;
                END
                

                这篇关于MySQL 自定义函数将罗马数字转为阿拉伯数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <bdo id='qRN5S'></bdo><ul id='qRN5S'></ul>
                    • <tfoot id='qRN5S'></tfoot><legend id='qRN5S'><style id='qRN5S'><dir id='qRN5S'><q id='qRN5S'></q></dir></style></legend>

                      1. <i id='qRN5S'><tr id='qRN5S'><dt id='qRN5S'><q id='qRN5S'><span id='qRN5S'><b id='qRN5S'><form id='qRN5S'><ins id='qRN5S'></ins><ul id='qRN5S'></ul><sub id='qRN5S'></sub></form><legend id='qRN5S'></legend><bdo id='qRN5S'><pre id='qRN5S'><center id='qRN5S'></center></pre></bdo></b><th id='qRN5S'></th></span></q></dt></tr></i><div id='qRN5S'><tfoot id='qRN5S'></tfoot><dl id='qRN5S'><fieldset id='qRN5S'></fieldset></dl></div>

                        <small id='qRN5S'></small><noframes id='qRN5S'>

                          <tbody id='qRN5S'></tbody>