romannumeral

package module
v1.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2021 License: Apache-2.0 Imports: 2 Imported by: 2

README

Go Roman Numerals

Go Reference codecov

Quickly and efficiently convert to and from roman numerals in Go.

A reliable module using the most efficient methods possible for converting between roman numerals and integers in Go. Algorithms adopted from here.

Benchmark Results
goos: darwin
goarch: arm64
pkg: github.com/brandenc40/romannumeral
BenchmarkIntToString-8          56474846                20.84 ns/op            0 B/op          0 allocs/op
BenchmarkIntToBytes-8           48157634                24.36 ns/op            0 B/op          0 allocs/op
BenchmarkStringToInt-8          17584252                67.28 ns/op            0 B/op          0 allocs/op
BenchmarkBytesToInt-8           18343551                64.77 ns/op            0 B/op          0 allocs/op
PASS
ok      github.com/brandenc40/romannumeral      6.111s
Example
package main

import (
	"fmt"
	rom "github.com/brandenc40/romannumeral"
)

func ExampleStringToInt() {
	integer, err := rom.StringToInt("IV")
	if err != nil {
		panic(err)
	}
	fmt.Println(integer == 4) // True
}

func ExampleBytesToInt() {
	integer, err := rom.BytesToInt([]byte("IV"))
	if err != nil {
		panic(err)
	}
	fmt.Println(integer == 4) // True
}

func ExampleIntToString() {
	roman, err := rom.IntToString(4)
	if err != nil {
		panic(err)
	}
	fmt.Println(roman == "IV") // True
}

func ExampleIntToBytes() {
	roman, err := rom.IntToBytes(4)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(roman) == "IV") // True
}

Documentation

Overview

Converts between integers and Roman Numeral strings.

Currently only supports Roman Numerals without viniculum (1-3999) and will throw an error for numbers outside of that range. See here for details on viniculum: https://en.wikipedia.org/wiki/Roman_numerals#Large_numbers

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	InvalidRomanNumeral = errors.New("invalid roman numeral")
	IntegerOutOfBounds  = errors.New("integer must be between 1 and 3999")
)

Functions

func BytesToInt added in v1.1.5

func BytesToInt(input []byte) (int, error)

BytesToInt converts a roman numeral byte array to an integer. Roman numerals for numbers outside of the range 1 to 3,999 will return an error. Nil or empty []byte will return 0 with no error thrown.

Example
input := []byte("IV")
integer, err := BytesToInt(input)
if err != nil {
	panic(err)
}
fmt.Println(integer == 4) // True
Output:

func IntToBytes added in v1.1.5

func IntToBytes(input int) ([]byte, error)

IntToBytes converts an integer value to a roman numeral byte array. An error is returned if the integer is not between 1 and 3999.

Example
roman, err := IntToBytes(4)
if err != nil {
	panic(err)
}
fmt.Println(string(roman) == "IV") // True
Output:

func IntToString added in v1.1.5

func IntToString(input int) (string, error)

IntToString converts an integer value to a roman numeral string. An error is returned if the integer is not between 1 and 3999.

Example
roman, err := IntToString(4)
if err != nil {
	panic(err)
}
fmt.Println(roman == "IV") // True
Output:

func StringToInt added in v1.1.5

func StringToInt(input string) (int, error)

StringToInt converts a roman numeral string to an integer. Roman numerals for numbers outside of the range 1 to 3,999 will return an error. Empty strings will return 0 with no error thrown.

Example
integer, err := StringToInt("IV")
if err != nil {
	panic(err)
}
fmt.Println(integer == 4) // True
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL