0

I searched for the answer and found an answer.I am adding here so that it will be easy for someone to find.This function will generate roman numerals.

CREATE FUNCTION `toRoman`(inArabic int unsigned) RETURNS varchar(15) CHARSET latin1 
BEGIN
DECLARE numeral CHAR(7) DEFAULT 'IVXLCDM';

DECLARE stringInUse CHAR(3);
DECLARE position tinyint DEFAULT 1;
DECLARE currentDigit tinyint;

DECLARE returnValue VARCHAR(15) DEFAULT '';

IF(inArabic > 3999) THEN RETURN 'overflow'; END IF;
IF(inArabic = 0) THEN RETURN 'N'; END IF;

WHILE position <= CEIL(LOG10(inArabic + .1)) DO
    SET currentDigit := MOD(FLOOR(inArabic / POW(10, position - 1)), 10);

    SET returnValue := CONCAT(
        CASE currentDigit
            WHEN 4 THEN CONCAT(SUBSTRING(numeral, position * 2 - 1, 1), SUBSTRING(numeral, position * 2, 1))
            WHEN 9 THEN CONCAT(SUBSTRING(numeral, position * 2 - 1, 1), SUBSTRING(numeral, position * 2 + 1, 1))
            ELSE CONCAT(
                REPEAT(SUBSTRING(numeral, position * 2, 1), currentDigit >= 5),
                REPEAT(SUBSTRING(numeral, position * 2 - 1, 1), MOD(currentDigit, 5))
            )
        END,
        returnValue);

    SET position := position + 1;
END WHILE;
RETURN returnValue;
END
  • specifically, see this answer https://stackoverflow.com/a/54529982/17389 – ysth Jun 25 '21 at 16:30
  • 2
    since the question to the answer mentioned by you is the inverse of what I was looking for, I did not read all the answers to that question.I have added the answer to my question so anybody looking for the answer can easily find it. – Aswin S Nath Jul 01 '21 at 05:13

0 Answers0