SpriterDrag

Integer to Roman

Aug 6th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///integer_to_roman(string);
  2.  
  3. /*
  4. **  Usage:
  5. **      integer_to_roman(string);
  6. **
  7. **  Argument:
  8. **      string      String to convert
  9. **
  10. **  Returns:
  11. **      The string converted into roman numerals.
  12. */
  13.  
  14. //Temporary string
  15. var str = "";
  16.  
  17. //Given input
  18. input = argument[0];
  19.  
  20. //First, let's check the input.
  21. if ((input < 1) || (input > 3999)) { //If the number is lower than 1 or greater than 3999.
  22.  
  23.     exit;
  24. }
  25.  
  26. //Otherwise, convert the input into roman numerals.
  27. else {
  28.  
  29.     //1000 into "M".
  30.     while (input >= 1000) { str += "M"; input -= 1000; }
  31.  
  32.     //900 into "CM".
  33.     while (input >= 900) { str += "CM"; input -= 900; }
  34.  
  35.     //500 into "D".
  36.     while (input >= 500) { str += "D"; input -= 500; }
  37.  
  38.     //400 into "CD".
  39.     while (input >= 400) { str += "CD"; input -= 400; }
  40.  
  41.     //100 into "C".
  42.     while (input >= 100) { str += "C"; input -= 100; }
  43.  
  44.     //90 into "XC".
  45.     while (input >= 90) { str += "XC"; input -= 90; }
  46.  
  47.     //50 into "L".
  48.     while (input >= 50) { str += "L"; input -= 50; }
  49.  
  50.     //40 into "XL".
  51.     while (input >= 40) { str += "XL"; input -= 40; }
  52.    
  53.     //10 into "X".
  54.     while (input >= 10) { str += "X"; input -= 10; }
  55.    
  56.     //9 into "IX".
  57.     while (input >= 9) { str += "IX"; input -= 9; }
  58.    
  59.     //5 into "V".
  60.     while (input >= 5) { str += "V"; input -= 5; }
  61.  
  62.     //4 into "IV".
  63.     while (input >= 4) { str += "IV"; input -= 4; }
  64.    
  65.     //1 into "I".
  66.     while (input >= 1) { str += "I"; input -= 1; }
  67. }
  68.  
  69. //Return the obtained string
  70. return str;
Add Comment
Please, Sign In to add comment