Posted By


cwylie0 on 01/19/11

Tagged


Statistics


Viewed 446 times
Favorited by 1 user(s)

Roman Numeral Converter


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.