Adriano R. Ferreira - Number.Roman-0.0.4

Documentation | Source

NAME

Number.Roman - Converting numbers to roman

SYNOPSIS

  // Functional example
  var xlii = Number.Roman.to_roman(42)

  // JSAN Example
  var jsan = new JSAN;
  jsan.use('Number.Roman', 'to_roman', 'is_roman');

  var x = to_roman(10) // return 'X'

  is_roman('XLI') // true
  is_roman('IIII') // false

DESCRIPTION

Converts numbers to roman, and that's it!

Actually we support two variants: roman numerals made up of simple letters I, V, X, L, C, D, M and roman numerals made up of Unicode roman numerals: '\u2160', '\u2164', '\u2169', '\u216C', '\u216D', '\u216E', '\u2180', '\u2181'.

The functions to_roman and is_roman accept a second argument which should be 'A' for roman numerals with ordinary ASCII characters and 'U' for roman numerals as Unicode strings. 'A' is the default.

FUNCTIONS

to_roman

  to_roman(n) // same as to_roman(n, 'A')
  to_roman(n, 'U')

  to_roman(32)  // is 'XXXII'
  to_roman(100) // is 'C'
  to_roman(0) // is ''
  to_roman(-1)  // throws an error
Converts numbers to roman numerals. The argument should be an integer (n==Math.round(n)) between 0 and 3999. Otherwise, an error is thrown.

is_roman

  is_roman(r)

  is_roman('X') // true
  is_roman('LL') // false
Answers if the string argument is a roman numeral. It is if it matches the regex

  /^M?M?M?(CM|CD|D?C?C?C?)?(XC|XL|L?X?X?X?)?(IX|IV|V?I?I?I?)?$/
in a case-insensitive manner.

EXPORTS

When used with JSAN this will export to_roman and is_roman on demand.

SEE ALSO

A very nice description of roman numerals is at http://en.wikipedia.org/wiki/Roman_numerals. History, alternate forms, modern usages are some of the issues discussed.

Roman numerals in Unicode are documented in http://www.unicode.org/charts/PDF/U2150.pdf which gives a chart of Unicode range 2150-218F. Not every browser renders them properly.

http://www.novaroma.org/via_romana/numbers.html

JSAN

CPAN modules:

AUTHOR

Adriano R. Ferreira, <ferreira@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2005, 2006 by Adriano R. Ferreira

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

// roman 0.0.1 July 07, 2005
// $Id: Roman.js,v 1.6 2005/07/16 02:41:16 ferreira Exp $

// Set up namepace
if (!Number) var Number = {};

Number.Roman = {};

Number.Roman.VERSION = '0.0.4';

// Exporter System for JSAN
Number.Roman.EXPORT_OK = [ 'to_roman', 'is_roman' ];

Number.Roman._romans = 
    [ 
      'M', 1000, 
      'CM', 900, 'D', 500,
      'CD', 400, 'C', 100,
      'XC', 90,  'L', 50,
      'XL', 40,  'X', 10,
      'IX', 9,   'V', 5,
      'IV', 4,   'I', 1 ]

Number.Roman._variants = {
    A: { // ascii variant (0-3999) IVXLCDM
      symbols: [ 'I', 'V', 'X', 'L', 'C', 'D', 'M' ],
      romans: [ 
          'M', 1000, 
          'CM', 900, 'D', 500,
          'CD', 400, 'C', 100,
          'XC', 90,  'L', 50,
          'XL', 40,  'X', 10,
          'IX', 9,   'V', 5,
          'IV', 4,   'I', 1 ],
      range: [ 0, 3999 ],
      regex: /^M?M?M?(CM|CD|D?C?C?C?)?(XC|XL|L?X?X?X?)?(IX|IV|V?I?I?I?)?$/i
    },
    U: { // unicode variant (0-39999)
      symbols: [ '\u2160', '\u2164', '\u2169', '\u216C', '\u216D', '\u216E',
                 '\u2180', '\u2181', '\u2182' ],
      romans: [ 
          '\u2182', 10000, '\u2181', 5000,
          '\u2180\u2181', 4000, '\u2180', 1000, 
          '\u216D\u2180', 900, '\u216E', 500,
          '\u216D\u216E', 400, '\u216D', 100,
          '\u2169\u216D', 90,  '\u216C', 50,
          '\u2169\u216C', 40,  '\u2169', 10,
          '\u2160\u2169', 9,   '\u2164', 5,
          '\u2160\u2164', 4,   '\u2160', 1 ],
      range: [ 0, 39999 ],
      regex: /^\u2182?\u2182?\u2182?(\u2180\u2182|\u2180\u2181|\u2181?\u2180?\u2180?\u2180?)(\u216D\u2180|\u216D\u216E|\u216E?\u216D?\u216D?\u216D?)?(\u2169\u216D|\u2169\u216\u216D|\u216\u216D?\u2169?\u2169?\u2169?)?(\u2160\u2169|\u2160\u2164|\u2164?\u2160?\u2160?\u2160?)?$/i

    }
};


Number.Roman.to_roman = function (n, v) {
    if (!v) v = 'A'
    var r_var = Number.Roman._variants[v]
    if (n!=Math.round(n)) 
        throw new Error('argument n='+n+' should be an integer')
    if (n<r_var.range[0] || n>r_var.range[1])
        throw new Error('argument n='+n+' should be in the range ('+r_var.range[0]+', '+r_var.range[1]+')')
    return Number.Roman._to_roman(n, r_var)
}

// this does not check the arguments
Number.Roman._to_roman = function (n, r_var) {
    var _romans = r_var.romans
    var r = ''
    var i = 0
    while (n>0) {
        if (n>=_romans[i+1]) {
            r += _romans[i]
            n -= _romans[i+1]
        } else {
            i += 2
        }
    }
    return r
}

Number.Roman.is_roman = function(r, v) {
    //if (r.length==0) return false
    //return /^M?M?M?(CM|CD|D?C?C?C?)?(XC|XL|L?X?X?X?)?(IX|IV|V?I?I?I?)?$/i.test(r)

    if (!v) v = 'A'
    var r_var = Number.Roman._variants[v]
    return r_var.regex.test(r)

}

/*

=head1 NAME

Number.Roman - Converting numbers to roman

=head1 SYNOPSIS

  // Functional example
  var xlii = Number.Roman.to_roman(42)

  // JSAN Example
  var jsan = new JSAN;
  jsan.use('Number.Roman', 'to_roman', 'is_roman');

  var x = to_roman(10) // return 'X'

  is_roman('XLI') // true
  is_roman('IIII') // false

=begin comment

  // Number.Roman.case = 'lc'
  // from_roman()

=end comment

=head1 DESCRIPTION

Converts numbers to roman, and that's it!

Actually we support two variants: roman numerals made up
of simple letters I, V, X, L, C, D, M and roman numerals
made up of Unicode roman numerals: '\u2160', '\u2164', 
'\u2169', '\u216C', '\u216D', '\u216E', '\u2180', '\u2181'.

The functions C<to_roman> and C<is_roman> accept a second
argument which should be 'A' for roman numerals with 
ordinary ASCII characters and 'U' for roman numerals as
Unicode strings. 'A' is the default.

=head2 FUNCTIONS

=over 4

=item B<to_roman>

  to_roman(n) // same as to_roman(n, 'A')
  to_roman(n, 'U')

  to_roman(32)  // is 'XXXII'
  to_roman(100) // is 'C'
  to_roman(0) // is ''
  to_roman(-1)  // throws an error

Converts numbers to roman numerals. The
argument should be an integer (C<n==Math.round(n)>)
between 0 and 3999. Otherwise, an error is thrown.

=item B<is_roman>

  is_roman(r)

  is_roman('X') // true
  is_roman('LL') // false

Answers if the string argument is a roman numeral.
It is if it matches the regex

  /^M?M?M?(CM|CD|D?C?C?C?)?(XC|XL|L?X?X?X?)?(IX|IV|V?I?I?I?)?$/

in a case-insensitive manner.

=back

=head2 EXPORTS

When used with C<JSAN> this will export C<to_roman> and
C<is_roman> on demand.

=head1 SEE ALSO

A very nice description of roman numerals is at 
L<http://en.wikipedia.org/wiki/Roman_numerals>. History,
alternate forms, modern usages are some of the issues
discussed.

Roman numerals in Unicode are documented in 
L<http://www.unicode.org/charts/PDF/U2150.pdf> which
gives a chart of Unicode range 2150-218F. Not every
browser renders them properly.

L<http://www.novaroma.org/via_romana/numbers.html>

C<JSAN>


CPAN modules: 

=over 4

=item *

L<http://search.cpan.org/dist/Roman/>,

=item *

L<http://search.cpan.org/dist/Math/Roman/>,

=item *

L<http://search.cpan.org/dist/Text/Roman/>,

=item *

L<http://search.cpan.org/search?query=roman&mode=module>

=back

=head1 AUTHOR

Adriano R. Ferreira, E<lt>ferreira@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2005, 2006 by Adriano R. Ferreira

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

*/