Your Ad Here

Posted By

cwylie0 on 01/19/11


Tagged

javascript roman numeral


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

cwylie0


Roman Numeral Converter


 / Published in: JavaScript
 

URL: http://javascript.about.com/library/blroman.htm

  1. // Convert to Roman Numerals
  2. // copyright 25th July 2005, by Stephen Chapman http://javascript.about.com
  3. // permission to use this Javascript on your web page is granted
  4. // provided that all of the code (including this copyright notice) is
  5. // used exactly as shown
  6. function roman(n,s) {
  7. var r = '';
  8. var d;
  9. var rn = new Array('IIII','V','XXXX','L','CCCC','D','MMMM');
  10. for (var i=0; i< rn.length; i++) {
  11. var x = rn[i].length+1;
  12. var d = n%x;
  13. r= rn[i].substr(0,d)+r;
  14. n = (n-d)/x;
  15. }
  16. if (s) {r=r.replace(/DCCCC/g,'CM');
  17. r=r.replace(/CCCC/g,'CD');
  18. r=r.replace(/LXXXX/g,'XC');
  19. r=r.replace(/XXXX/g,'XL');
  20. r=r.replace(/VIIII/g,'IX');
  21. r=r.replace(/IIII/g,'IV');}
  22. return r;
  23. }

Report this snippet  

You need to login to post a comment.