So I was browsing the web for a way to convert an integer into a Roman number. Surprisingly there was not any for GML and I only found one for Javascript.
Said code can be found here.http://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascriptAfter that, I tried to port it into GML which I did succesfully after a lot of errors///integer_to_roman(string);
/*
** Usage:
** integer_to_roman(string);
**
** Argument:
** string String to convert
**
** Returns:
** The string converted into roman numerals.
*/
//Temporary string
var str = "";
//Given input
input = argument[0];
//First, let's check the input.
if ((input < 1) || (input > 3999)) { //If the number is lower than 1 or greater than 3999.
exit;
}
//Otherwise, convert the input into roman numerals.
else {
//1000 into "M".
while (input >= 1000) { str += "M"; input -= 1000; }
//900 into "CM".
while (input >= 900) { str += "CM"; input -= 900; }
//500 into "D".
while (input >= 500) { str += "D"; input -= 500; }
//400 into "CD".
while (input >= 400) { str += "CD"; input -= 400; }
//100 into "C".
while (input >= 100) { str += "C"; input -= 100; }
//90 into "XC".
while (input >= 90) { str += "XC"; input -= 90; }
//50 into "L".
while (input >= 50) { str += "L"; input -= 50; }
//40 into "XL".
while (input >= 40) { str += "XL"; input -= 40; }
//10 into "X".
while (input >= 10) { str += "X"; input -= 10; }
//9 into "IX".
while (input >= 9) { str += "IX"; input -= 9; }
//5 into "V".
while (input >= 5) { str += "V"; input -= 5; }
//4 into "IV".
while (input >= 4) { str += "IV"; input -= 4; }
//1 into "I".
while (input >= 1) { str += "I"; input -= 1; }
}
//Return the obtained string
return str;
I wonder if there is a way to do that without so many while loops.
EDIT: The Javascript one did have only one while loop, but I wonder if it's possible to do without any while loops.Your method makes more sense to humans, but there's also this way:
https://discuss.leetcode.com/topic/12384/simple-solution/16If the input is equal to 4000 you get a IV with a _ above. But how can that be coded.