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

Roman Numerals

Philip McGrath <philip at philipmcgrath dot com>

This library provides utilities for parsing and writing Roman numerals.

procedure

(number->roman number)  (and/c string? #rx"(?i:[mdclxvi]+)")

  number : (and/c natural-number/c (not/c 0))
Returns the Roman numeral representation of number as a string. The string will be in lower case if (current-roman-numeral-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"

parameter

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

(current-roman-numeral-case case)  void?
  case : (or/c 'lower 'upper)
Specifies whether the string returned by number->roman is upper case or lower case.

procedure

(roman->number numeral)  (and/c natural-number/c (not/c 0))

  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"

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

roman->number: malformed Roman numeral string

  given: "cdm"