// Package romannumerals is for handling roman numbers
package romannumerals
import (
"fmt"
"math"
)
var romanNum = map[int]string{
1000: "M", 500: "D", 100: "C", 50: "L", 10: "X", 5: "V", 1: "I",
}
// ToRomanNumeral converts arabic numbers to roman ones
func ToRomanNumeral(arabic int) (roman string, err error) {
if arabic > 3000 {
return "", fmt.Errorf("bigger than 3000")
}
if arabic <= 0 {
return "", fmt.Errorf("the input have to be positive")
}
// place value starts at 10^3
for pv := 3; pv >= 0; pv-- {
pow := int(math.Pow10(pv))
switch arabic / pow {
case 1:
roman += romanNum[pow]
case 2:
roman += romanNum[pow] + romanNum[pow]
case 3:
roman += romanNum[pow] + romanNum[pow] + romanNum[pow]
case 4:
roman += romanNum[pow] + romanNum[pow*5]
case 5:
roman += romanNum[pow*5]
case 6:
roman += romanNum[pow*5] + romanNum[pow]
case 7:
roman += romanNum[pow*5] + romanNum[pow] + romanNum[pow]
case 8:
roman += romanNum[pow*5] + romanNum[pow] + romanNum[pow] + romanNum[pow]
case 9:
roman += romanNum[pow] + romanNum[pow*10]
}
// this one only makes sense because the int truncate
arabic -= arabic / pow * pow
}
return roman, nil
}