Roman

Package Version Hex Docs

A Gleam library for converting between Roman numerals and integers, with support for both string and structured representations.

Installation

gleam add roman@1

Usage

Types

The library provides several types for working with Roman numerals:

Converting Strings to Roman Numerals

import roman

// Convert a string to Roman numerals
let result = roman.string_to_roman("xlii")
// Returns: Ok([X, L, I, I])

// Invalid characters return an error
let error = roman.string_to_roman("abc")
// Returns: Error(InvalidNumeralCharInput)

Converting Roman Numerals to Integers

import roman

// First convert string to Roman, then to integer
let roman_numerals = roman.string_to_roman("xlii")
case roman_numerals {
  Ok(numerals) -> {
    let value = roman.roman_to_int(numerals)
    // Returns: 42
  }
  Error(_) -> // handle error
}

Converting Integers to Roman Numerals

import roman

// Convert integer to Roman numerals
let roman_numerals = roman.int_to_roman(42)
// Returns: Ok([X, L, I, I])

// Zero and negative numbers return None
let invalid = roman.int_to_roman(0)
// Returns: Error(ZeroOrNegativeIntegerInput)

Converting Roman Numerals to Strings

import roman

// Convert Roman numerals back to string
let roman_numerals = roman.int_to_roman(42)
case roman_numerals {
  Some(numerals) -> {
    let roman_string = roman.roman_to_string(numerals)
    // Returns: "xlii"
  }
  None -> // handle invalid input
}

Supported Roman Numerals

The library supports all standard Roman numerals:

Features

Error Handling

The library provides proper error handling:

Documentation

Further documentation can be found at https://hexdocs.pm/gleam_roman/.

Search Document