Roman Numerals
number->roman
current-roman-numeral-case
roman->number
7.1

Roman Numerals

Philip McGrath <philip at philipmcgrath dot com>

This library provides utilities for parsing and writing Roman numerals.

procedure

(number->roman number [#:case output-case])

  (and/c string? #rx"^(?i:[mdclxvi]+)$")
  number : exact-positive-integer?
  output-case : (or/c 'lower 'upper)
   = (current-roman-numeral-case)
Returns the Roman numeral representation of number as a string. The string will be in lower case if output-case is 'lower (the default) or in upper case otherwise.

Examples:
> (number->roman 1990)

"mcmxc"

> (number->roman 1666)

"mdclxvi"

> (parameterize ([current-roman-numeral-case 'upper])
    (number->roman 1666))

"MDCLXVI"

Changed in version 0.1 of package roman-numeral: Added #:case argument.

parameter

(current-roman-numeral-case)  (or/c 'lower 'upper)

(current-roman-numeral-case output-case)  void?
  output-case : (or/c 'lower 'upper)
 = 'lower
Specifies whether strings returned by number->roman use upper case or lower case when no #:case argument is given.

procedure

(roman->number numeral)  exact-positive-integer?

  numeral : (and/c string? #rx"^(?i:[mdclxvi]+)$")
Returns the number represented by the Roman numeral string numeral.

Roman numeral strings that do not use the standard subtractive forms are supported, but numeral must be a valid Roman numeral, not simply an arbitrary sequence of the characters listed in the contract.

Examples:
> (roman->number "mcmiv")

1904

> (roman->number "mcMlIv") ; case is ignored

1954

; non-standard Roman numerals
> (roman->number "iiii")

4

> (number->roman 4)

"iv"

> (roman->number "xiix")

18

> (roman->number "iixx")

18

> (number->roman 18)

"xviii"

> (roman->number "MDCDIII")

1903

> (number->roman 1903)

"mcmiii"

> (roman->number "MDCCCCX")

1910

> (number->roman 1910)

"mcmx"

> (eval:error (roman->number "cdm")) ; not a valid Roman numeral

roman->number: malformed Roman numeral string

  given: "cdm"