dsparling / romannumerals

jQuery plugin - converts arabic numbers to roman numerals

This URL has Read+Write access

dsparling imbp (author)
Wed Jun 11 11:47:07 -0700 2008
romannumerals / jquery.romannumerals.js
100644 79 lines (71 sloc) 2.252 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* jQuery romannumerals - Roman Numberl converter plugin - v 1.0.0
* http://plugins.jquery.com/project/romannumerals
*
* Copyright (c) 2007-2008 Doug Sparling <http://www.dougsparling.com>
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*/
(function($) {
 
  $.fn.romannumerals = function(options) {
    var version = '1.0.0';
 
    // options
    var opts = $.extend({}, $.fn.romannumerals.defaults, options);
         
    // iterate convert each matched element
    return this.each(function() {
      $this = $(this);
      // build element specific options
      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
      // update element styles
      $this.css({
        fontFamily: o.fontFamily,
        fontSize: o.fontSize,
        color: o.foreground,
        backgroundColor: o.background
      });
      var number = $this.html();
      // call arabicToRoman function
      var romanNumber = $.fn.romannumerals.arabicToRoman(number);
      $this.html(romanNumber);
    });
  };
       
  //
  // define and expose arabicToRoman function (4999 max)
  // http://groups.google.com/group/pl.comp.lang.javascript/msg/c8991a94f77c98ce
  $.fn.romannumerals.arabicToRoman = function(N,s,b,a,o,t) {
    for(s=b='',a=5;N;b++,a^=7) {
      for(o=N%a,N=N/a^0;o--;) {
        s='IVXLCDM'.charAt(o>2?b+N-(N&=~1)+(o=1):b)+s;
      }
    }
    return s
  }
 
  //
  // define and expose arabicToRoman function
  // http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter
  $.fn.romannumerals.arabicToRoman2 = function(N,s,b,a,o,t) {
    t=N/1e3|0;N%=1e3;
    for(s=b='',a=5;N;b++,a^=7) {
      for(o=N%a,N=N/a^0;o--;) {
        s='IVXLCDM'.charAt(o>2?b+N-(N&=~1)+(o=1):b)+s;
      }
    }
    return Array(t+1).join('M')+s;
  }
 
  //
  // plugin defaults
  //
  $.fn.romannumerals.defaults = {
    fontFamily: '',
    fontSize: '',
    foreground: '',
    background: ''
  };
})(jQuery);
 
/*
* http://en.wikipedia.org/wiki/Roman_numerals
* http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter
* http://groups.google.com/group/pl.comp.lang.javascript/msg/c8991a94f77c98ce
* http://ostermiller.org/calc/roman.html
* http://www.ivtech.com/roman/faq.php
*/