wordnumber

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2021 License: MIT Imports: 5 Imported by: 2

README

Word-Number

This package provides a methods for converting numbers to/from various 'word' representations.

Currently, that means cardinal and ordinal numbers in british english using the short scale.

Roman numerals are extended using the Vinculum system (allowing numbers from 4k to 4M-1).

You can use wordnumber as a package in your own project, or as a command line application.

Installation

For use as a library:

  go get github.com/mantidtech/wordnumber

Install the command line application:

  go install github.com/mantidtech/wordnumber/cmd/wordnumber@latest

Or:

  go clone ssh://sooty@github.com/mantidtech/wordnumber.git
  cd wordnumber
  ./build.sh install

Command-Line

wordnumber converts an integer to various 'word' formats (and back)

Usage: wordnumber [-i] [-c|-o|-s|-r] <int>
       wordnumber -v
       wordnumber -h
  -h, --help            this message
  -c, --cardinal        display the number given in cardinal form (eg 12 -> 'twelve') (default true)
  -o, --ordinal         display the number given in ordinal form (eg 12 -> 'twelfth')
  -s, --short-ordinal   display the number given in short ordinal form (eg 12 -> '12th')
  -r, --roman           display the number given in roman form (eg 12 -> 'XII')
  -i, --inverse         perform the inverse operation (eg with -i -c 'twelve' -> 12)
  -v, --version         display the version

Types

Cardinal

The names for numbers

eg

    1 -> one
  812 -> eight hundred and twelve
-1000 -> negative one thousand
Limits

For now the numbers used for input are taken as ints, so the following limits apply:

  • Low - MinInt64
  • High - MaxInt64
Roman

Standard Roman numerals cover numbers in the range 1-3999 (I to MMMCMXCIX).

The Vinculum system uses bars (a vinculum) above numerals to denote x1000 multiplier to numerals, thus extending the range to 3,999,999 (M̄M̄M̄C̄M̄X̄C̄MX̄CMXCIX)

Fractions are not yet supported.

eg

    1 -> I
   45 -> XLV
 3999 -> MMMCMXCIX
52010 -> L̄MMX
  • Low - 0 (Roman numerals start at one, though this library provides nulla for 0)
  • High - 4M-1
Ordinal

The names for a position represented by a number

eg

    1 -> first
    2 -> second
  812 -> eight hundred and twelfth

For now the numbers used for input are taken as ints, so the following limits apply:

  • Low - 0 (negative ordinal numbers don't make sense)
  • High - MaxInt64
Short Ordinal

The names for a position represented by a number, using numerals and suffix

eg

    1 -> 1st
    2 -> 2nd
  812 -> 812th

For now the numbers used for input are taken as ints, so the following limits apply:

  • Low - 0 (negative ordinal numbers don't make sense)
  • High - MaxInt64

Library

The following methods are defined to perform the operation detailed above.

  • RomanToInt(s string) (res int, err error)
  • IntToRoman(i int) (string, error)
  • CardinalToInt(s string) (int, error)
  • IntToCardinal(i int) string
  • OrdinalShortToInt(s string) (int, error)
  • IntToOrdinalShort(i int) (string, error)
  • OrdinalToInt(s string) (int, error)
  • IntToOrdinal(i int) (string, error)

Known Bugs

  • Roman 4000 should maybe/probably be ĪV̄ rather than MV̄

Todo

  • magnitude style numbers, eg giga- milli-
  • fractional values
  • have a selectable way to represent roman numerals between 4,000->3,999,999 (not all fonts deal with the unicode combining bar (U+0304) well)

(c) 2021 - Julian Peterson <code@mantid.org> - MIT Licensed

Documentation

Overview

Package wordnumber provides a set of methods to convert numbers into various 'word' representations

Currently that means cardinal and ordinal numbers in british english using the short scale, plus roman numerals

Index

Constants

View Source
const MaxRoman = 1_000_000*4 - 1

MaxRoman is the largest roman numeral that can be represented

Variables

This section is empty.

Functions

func CardinalToInt

func CardinalToInt(s string) (int, error)

CardinalToInt converts a word description for a number into an int

eg. "one hundred and four" => 104

func IntToCardinal

func IntToCardinal(i int) string

IntToCardinal returns the word form of the given number

eg. 37 => "thirty seven"

func IntToOrdinal

func IntToOrdinal(i int) (string, error)

IntToOrdinal returns the given number as a ordinal word, eg 13 => thirteenth

func IntToOrdinalShort

func IntToOrdinalShort(i int) (string, error)

IntToOrdinalShort returns the given number as an ordinal number in short form, eg 13 => 13th

func IntToRoman

func IntToRoman(i int) (string, error)

IntToRoman returns the given number in roman numerals

eg. 54 => "LIV"

func OrdinalShortToInt

func OrdinalShortToInt(s string) (int, error)

OrdinalShortToInt converts a word description for a short ordinal number into an int

eg. "104th" => 104

func OrdinalToInt

func OrdinalToInt(s string) (int, error)

OrdinalToInt converts a word description for an ordinal number into an int

eg. "one hundred and fourth" => 104

func PrintedLen

func PrintedLen(s string) (res int)

PrintedLen returns the printed length of a string, ie the number of cells on a terminal it uses

this method is useful because the extended roman numerals use combining unicode characters to render the over-bar. That means

len("M̄") == 2 // ie the number of runes in the string

and

PrintedLen("M̄") == 1

func RomanToInt

func RomanToInt(s string) (res int, err error)

RomanToInt returns the decimal value of the given roman numeral

func Version

func Version() string

Version returns the compiled version information for this library

Types

type Error

type Error string

Error is the error type returned from this package

const (
	ErrNotImplemented    Error = "method '%s' is not yet implemented"
	ErrOutOfRange        Error = "number out of range"
	ErrNegative          Error = "cannot represent negative numbers"
	ErrFmtOverflow       Error = "cannot represent numbers greater than %d"
	ErrUnknownConversion Error = "unknown conversion method %s"
	ErrParseCardinal     Error = "unknown word '%s' in expression, can't parse as a cardinal number"
	ErrParseOrdinal      Error = "unknown word '%s' in expression, can't parse as an ordinal number"
	ErrParseShortOrdinal Error = "can't parse '%s' as a short ordinal number"
	ErrBadRoman          Error = "unexpected character parsing roman numerals"
	ErrParseInit         Error = "could not process '%s' as an integer"
	ErrNoInput           Error = "no input was provided"
)

Possible errors returned from this package

func (Error) Error

func (e Error) Error() string

Error implements the Error interface

func (Error) With

func (e Error) With(p ...interface{}) Error

With supplies parameters to error messages

type Group

type Group []int

Group is a division of a number into sets of numerals. different translations of numbers are easiest when batched into certain group sizes

func GetGroups

func GetGroups(i int, s int) (g Group)

GetGroups divides a number into groups of a given size (number of digits) note: don't use s < 1, that's bad

Directories

Path Synopsis
cmd
example
example printing of a selection of numbers in formats provided by the wordnumber library
example printing of a selection of numbers in formats provided by the wordnumber library
wordnumber
wordnumber converts an integer to various 'word' formats (and back) Usage: wordnumber [-i] [-c|-o|-s|-r] <int> wordnumber -v wordnumber -h -h, --help this message -c, --cardinal display the number given in cardinal form (eg 12 -> 'twelve') (default true) -o, --ordinal display the number given in ordinal form (eg 12 -> 'twelfth') -s, --short-ordinal display the number given in short ordinal form (eg 12 -> '12th') -r, --roman display the number given in roman form (eg 12 -> 'XII') -i, --inverse perform the inverse operation (eg with -i -c 'twelve' -> 12) -v, --version display the version
wordnumber converts an integer to various 'word' formats (and back) Usage: wordnumber [-i] [-c|-o|-s|-r] <int> wordnumber -v wordnumber -h -h, --help this message -c, --cardinal display the number given in cardinal form (eg 12 -> 'twelve') (default true) -o, --ordinal display the number given in ordinal form (eg 12 -> 'twelfth') -s, --short-ordinal display the number given in short ordinal form (eg 12 -> '12th') -r, --roman display the number given in roman form (eg 12 -> 'XII') -i, --inverse perform the inverse operation (eg with -i -c 'twelve' -> 12) -v, --version display the version

Jump to

Keyboard shortcuts

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