numeric

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2018 License: MIT Imports: 0 Imported by: 1

README

go-numeric

A library that provides helper functions to deal with runes that represent numeric values, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-numeric

GoDoc

Example

Here is an example using the numeric.Int64() func:

var r rune = '2'
//var r rune = '۲'
//var r rune = 'Ⅱ'
//var r rune = 'ⅱ'
//var r rune = '〢'
//var r rune = '𐏒'


i64, err := numeric.Int64(test.Rune)

if nil != err {
	switch err.(type) {
	case numeric.NotNumericComplainer:
		fmt.Println("The rune does not represent a numeric value.")
	case numeric.NotInRangeComplainer:
		fmt.Println("Although the rune represents a numeric value, it cannot be represents as an int64.")
	default:
		fmt.Println("This should never happen!")
	}
}

fmt.Printf("The numeric value that rune represents is: %d", i64)

Here is an example using the numeric.Frac64() func:

var r rune = '½'
//var r rune = '⅗'
//var r rune = 'Ⅵ'
//var r rune = 'ⅺ'


n64, d64, err := numeric.Frac64(test.Rune)

if nil != err {
	switch err.(type) {
	case numeric.NotNumericComplainer:
		fmt.Println("The rune does not represent a numeric value.")
	default:
		fmt.Println("This should never happen!")
	}
}

fmt.Printf("The numeric value fraction that rune represents has a numerator %d and denominator", n64, d64)

Documentation

Overview

Package numeric provides helper functions to deal with runes that represent numeric values.

Index

Examples

Constants

View Source
const (
	DigitZero  = '\u0030' // Digit Zero  (0)
	DigitOne   = '\u0031' // Digit One   (1)
	DigitTwo   = '\u0032' // Digit Two   (2)
	DigitThree = '\u0033' // Digit Three (3)
	DigitFour  = '\u0034' // Digit Four  (4)
	DigitFive  = '\u0035' // Digit Five  (5)
	DigitSix   = '\u0036' // Digit Six   (6)
	DigitSeven = '\u0037' // Digit Seven (7)
	DigitEight = '\u0038' // Digit Eight (8)
	DigitNine  = '\u0039' // Digit Nine  (9)

	ArabicIndicDigitZero  = '\u0660' // Arabic-Indic Digit Zero  (٠)
	ArabicIndicDigitOne   = '\u0661' // Arabic-Indic Digit One   (١)
	ArabicIndicDigitTwo   = '\u0662' // Arabic-Indic Digit Two   (٢)
	ArabicIndicDigitThree = '\u0663' // Arabic-Indic Digit Three (٣)
	ArabicIndicDigitFour  = '\u0664' // Arabic-Indic Digit Four  (٤)
	ArabicIndicDigitFive  = '\u0665' // Arabic-Indic Digit Five  (٥)
	ArabicIndicDigitSix   = '\u0666' // Arabic-Indic Digit Six   (٦)
	ArabicIndicDigitSeven = '\u0667' // Arabic-Indic Digit Seven (٧)
	ArabicIndicDigitEight = '\u0668' // Arabic-Indic Digit Eight (٨)
	ArabicIndicDigitNine  = '\u0669' // Arabic-Indic Digit Nine  (٩)

	ExtendedArabicIndicDigitZero  = '\u06F0' // Extended Arabic-Indic Digit Zero  (۰)
	ExtendedArabicIndicDigitOne   = '\u06F1' // Extended Arabic-Indic Digit One   (۱)
	ExtendedArabicIndicDigitTwo   = '\u06F2' // Extended Arabic-Indic Digit Two   (۲)
	ExtendedArabicIndicDigitThree = '\u06F3' // Extended Arabic-Indic Digit Three (۳)
	ExtendedArabicIndicDigitFour  = '\u06F4' // Extended Arabic-Indic Digit Four  (۴)
	ExtendedArabicIndicDigitFive  = '\u06F5' // Extended Arabic-Indic Digit Five  (۵)
	ExtendedArabicIndicDigitSix   = '\u06F6' // Extended Arabic-Indic Digit Six   (۶)
	ExtendedArabicIndicDigitSeven = '\u06F7' // Extended Arabic-Indic Digit Seven (۷)
	ExtendedArabicIndicDigitEight = '\u06F8' // Extended Arabic-Indic Digit Eight (۸)
	ExtendedArabicIndicDigitNine  = '\u06F9' // Extended Arabic-Indic Digit Nine  (۹)

	SuperscriptZero  = '\u2070' // Superscript Zero  (⁰)
	SuperscriptOne   = '\u00B9' // Superscript One   (¹)
	SuperscriptTwo   = '\u00B2' // Superscript Two   (²)
	SuperscriptThree = '\u00B3' // Superscript Three (³)
	SuperscriptFour  = '\u2074' // Superscript Four  (⁴)
	SuperscriptFive  = '\u2075' // Superscript Five  (⁵)
	SuperscriptSix   = '\u2076' // Superscript Six   (⁶)
	SuperscriptSeven = '\u2077' // Superscript Seven (⁷)
	SuperscriptEight = '\u2078' // Superscript Eight (⁸)
	SuperscriptNine  = '\u2079' // Superscript Nine  (⁹)

	SubscriptZero  = '\u2080' // Subscript Zero  (₀)
	SubscriptOne   = '\u2081' // Subscript One   (₁)
	SubscriptTwo   = '\u2082' // Subscript Two   (₂)
	SubscriptThree = '\u2083' // Subscript Three (₃)
	SubscriptFour  = '\u2084' // Subscript Four  (₄)
	SubscriptFive  = '\u2085' // Subscript Five  (₅)
	SubscriptSix   = '\u2086' // Subscript Six   (₆)
	SubscriptSeven = '\u2087' // Subscript Seven (₇)
	SubscriptEight = '\u2088' // Subscript Eight (₈)
	SubscriptNine  = '\u2089' // Subscript Nine  (₉)

	VulgarFractionOneQuarter    = '\u00BC' // Vulgar Fraction One Quarter    (¼)
	VulgarFractionOneHalf       = '\u00BD' // Vulgar Fraction One Half       (½)
	VulgarFractionThreeQuarters = '\u00BE' // Vulgar Fraction Three Quarters (¾)

	VulgarFractionOneSeventh = '\u2150' // Vulgar Fraction One Seventh    (⅐)

	VulgarFractionOneNinth = '\u2151' // Vulgar Fraction One Ninth      (⅑)

	VulgarFractionOneTenth = '\u2152' // Vulgar Fraction One Tenth      (⅒)

	VulgarFractionZeroThirds = '\u2189' // Vulgar Fraction Zero Thirds    (↉)
	VulgarFractionOneThird   = '\u2153' // Vulgar Fraction One Third      (⅓)
	VulgarFractionTwoThirds  = '\u2154' // Vulgar Fraction Two Thirds     (⅔)

	VulgarFractionOneFifth    = '\u2155' // Vulgar Fraction One Fifth      (⅕)
	VulgarFractionTwoFifths   = '\u2156' // Vulgar Fraction Two Fifths     (⅖)
	VulgarFractionThreeFifths = '\u2157' // Vulgar Fraction Three Fifths   (⅗)
	VulgarFractionFourFifths  = '\u2158' // Vulgar Fraction Four Fifths    (⅘)

	VulgarFractionOneSixth   = '\u2159' // Vulgar Fraction One Sixth      (⅙)
	VulgarFractionFiveSixths = '\u215A' // Vulgar Fraction Five Sixths    (⅚)

	VulgarFractionOneEighth    = '\u215B' // Vulgar Fraction One Eighth     (⅛)
	VulgarFractionThreeEighths = '\u215C' // Vulgar Fraction Three Eighths  (⅜)
	VulgarFractionFiveEighths  = '\u215D' // Vulgar Fraction Five Eighths   (⅝)
	VulgarFractionSevenEighths = '\u215E' // Vulgar Fraction Seven Eighths  (⅞)

	FractionNumeratorOne = '\u215F' // Fraction Numerator One         (⅟)

	RomanNumeralOne         = '\u2160' // Roman Numeral One          (Ⅰ)
	RomanNumeralTwo         = '\u2161' // Roman Numeral Two          (Ⅱ)
	RomanNumeralThree       = '\u2162' // Roman Numeral Three        (Ⅲ)
	RomanNumeralFour        = '\u2163' // Roman Numeral Four         (Ⅳ)
	RomanNumeralFive        = '\u2164' // Roman Numeral Five         (Ⅴ)
	RomanNumeralSix         = '\u2165' // Roman Numeral Six          (Ⅵ)
	RomanNumeralSeven       = '\u2166' // Roman Numeral Seven        (Ⅶ)
	RomanNumeralEight       = '\u2167' // Roman Numeral Eight        (Ⅷ)
	RomanNumeralNine        = '\u2168' // Roman Numeral Nine         (Ⅸ)
	RomanNumeralTen         = '\u2169' // Roman Numeral Ten          (Ⅹ)
	RomanNumeralEleven      = '\u216A' // Roman Numeral Eleven       (Ⅺ)
	RomanNumeralTwelve      = '\u216B' // Roman Numeral Twelve       (Ⅻ)
	RomanNumeralFifty       = '\u216C' // Roman Numeral Fifty        (Ⅼ)
	RomanNumeralOneHundred  = '\u216D' // Roman Numeral One Hundred  (Ⅽ)
	RomanNumeralFiveHundred = '\u216E' // Roman Numeral Five Hundred (Ⅾ)
	RomanNumeralOneThousand = '\u216F' // Roman Numeral One Thousand (Ⅿ)

	SmallRomanNumeralOne         = '\u2170' // Small Roman Numeral One          (ⅰ)
	SmallRomanNumeralTwo         = '\u2171' // Small Roman Numeral Two          (ⅱ)
	SmallRomanNumeralThree       = '\u2172' // Small Roman Numeral Three        (ⅲ)
	SmallRomanNumeralFour        = '\u2173' // Small Roman Numeral Four         (ⅳ)
	SmallRomanNumeralFive        = '\u2174' // Small Roman Numeral Five         (ⅴ)
	SmallRomanNumeralSix         = '\u2175' // Small Roman Numeral Six          (ⅵ)
	SmallRomanNumeralSeven       = '\u2176' // Small Roman Numeral Seven        (ⅶ)
	SmallRomanNumeralEight       = '\u2177' // Small Roman Numeral Eight        (ⅷ)
	SmallRomanNumeralNine        = '\u2178' // Small Roman Numeral Nine         (ⅸ)
	SmallRomanNumeralTen         = '\u2179' // Small Roman Numeral Ten          (ⅹ)
	SmallRomanNumeralEleven      = '\u217A' // Small Roman Numeral Eleven       (ⅺ)
	SmallRomanNumeralTwelve      = '\u217B' // Small Roman Numeral Twelve       (ⅻ)
	SmallRomanNumeralFifty       = '\u217C' // Small Roman Numeral Fifty        (ⅼ)
	SmallRomanNumeralOneHundred  = '\u217D' // Small Roman Numeral One Hundred  (ⅽ)
	SmallRomanNumeralFiveHundred = '\u217E' // Small Roman Numeral Five Hundred (ⅾ)
	SmallRomanNumeralOneThousand = '\u217F' // Small Roman Numeral One Thousand (ⅿ)

	RomanNumeralOneThousandCD      = '\u2180' // Roman Numeral One Thousand C D     (ↀ)
	RomanNumeralFiveThousand       = '\u2181' // Roman Numeral Five Thousand        (ↁ)
	RomanNumeralTenThousand        = '\u2182' // Roman Numeral Ten Thousand         (ↂ)
	RomanNumeralReservedOneHundred = '\u2183' // Roman Numeral Reversed One Hundred (Ↄ)
	LatinSmallLetterReversedC      = '\u2184' // Latin Small Letter Reversed C      (ↄ)
	RomanNumeralSixLateForm        = '\u2185' // Roman Numeral Six Late Form        (ↅ)
	RomanNumeralFiftyEarlyForm     = '\u2186' // Roman Numeral Fifty Early Form     (ↆ)
	RomanNumeralFiftyThousand      = '\u2187' // Roman Numeral Fifty Thousand       (ↇ)
	RomanNumeralOneHundredThousand = '\u2188' // Roman Numeral One Hundred Thousand (ↈ)

	TurnedDigitTwo   = '\u218A' // Turned Digit Two   (Duodecimal 10)
	TurnedDigitThree = '\u218B' // Turned Digit Three (Duodecimal 11)

	HangzhouNumeralOne   = '\u3021' // Hangzhou Numeral One    (〡)
	HangzhouNumeralTwo   = '\u3022' // Hangzhou Numeral Two    (〢)
	HangzhouNumeralThree = '\u3023' // Hangzhou Numeral Three  (〣)
	HangzhouNumeralFour  = '\u3024' // Hangzhou Numeral Four   (〤)
	HangzhouNumeralFive  = '\u3025' // Hangzhou Numeral Five   (〥)
	HangzhouNumeralSix   = '\u3026' // Hangzhou Numeral Six    (〦)
	HangzhouNumeralSeven = '\u3027' // Hangzhou Numeral Seven  (〧)
	HangzhouNumeralEight = '\u3028' // Hangzhou Numeral Eight  (〨)
	HangzhouNumeralNine  = '\u3029' // Hangzhou Numeral Nine   (〩)

	HangzhouNumeralTen    = '\u3038' // Hangzhou Numeral Ten    (〸)
	HangzhouNumeralTwenty = '\u3039' // Hangzhou Numeral Twenty (〹)

	HangzhouNumeralThirty = '\u303A' // Hangzhou Numeral Thrity (〺)

	OldPersianNumberOne     = '\U000103D1' // Old Persian Number One     (𐏑)
	OldPersianNumberTwo     = '\U000103D2' // Old Persian Number Two     (𐏒)
	OldPersianNumberTen     = '\U000103D3' // Old Persian Number Ten     (𐏓)
	OldPersianNumberTwenty  = '\U000103D4' // Old Persian Number Twenty  (𐏔)
	OldPersianNumberHundred = '\U000103D5' // Old Persian Number Hundred (𐏕)
)

Variables

This section is empty.

Functions

func Frac64

func Frac64(r rune) (numerator int64, denominator int64, err error)

Frac64 tries to convert the numeric value that a rune represents to an fraction made of an int64 numerator and int64 denominator.

Note, of course, not all runes represent numeric values. In those cases Frac64 returns an error of type NotNumericComplainer.

func Int64

func Int64(r rune) (int64, error)

Int64 tries to convert the numeric value that a rune represents to an int64.

Note, of course, not all runes represent numeric values. In those cases Int64 returns an error of type NotNumericComplainer.

Also, if the rune is numeric but cannot be represented as an int64, then Int64 returns an error of type NotInRangeComplainer.

func IsHangzhouNumeral

func IsHangzhouNumeral(r rune) bool

IsHangzhouNumeral returns true if the rune is a Hangzhou Numeral, and returns false otherwise.

Namely:

Hangzhou Numeral One (〡)

Hangzhou Numeral Two (〢)

Hangzhou Numeral Three (〣)

Hangzhou Numeral Four (〤)

Hangzhou Numeral Five (〥)

Hangzhou Numeral Six (〦)

Hangzhou Numeral Seven (〧)

Hangzhou Numeral Eight (〨)

Hangzhou Numeral Nine (〩)

Hangzhou Numeral Ten (〸)

Hangzhou Numeral Twenty (〹)

Hangzhou Numeral Thrity (〺)

func IsNumeric

func IsNumeric(r rune) bool

IsNumeric return true it the value the rune represents is numeric, else returns false.

For example, it would return true for '2', '⅗' and 'Ⅶ'; but would return false for '&', '😀' and '?'.

func IsOldPersianNumber

func IsOldPersianNumber(r rune) bool

IsOldPersianNumber returns true if the rune is an Old Persian Number, and returns false otherwise.

Namely:

Old Persian Number One (𐏑)

Old Persian Number Two (𐏒)

Old Persian Number Ten (𐏓)

Old Persian Number Twenty (𐏔)

Old Persian Number Hundred (𐏕)

func Uint64

func Uint64(r rune) (uint64, error)

Uint64 tries to convert the numeric value that a rune represents to an uint64.

Note, of course, not all runes represent numeric values. In those cases Uint64 returns an error of type NotNumericComplainer.

Also, if the rune is numeric but cannot be represented as an uint64, then Uint64 returns an error of type NotInRangeComplainer.

Types

type NotInRangeComplainer

type NotInRangeComplainer interface {
	error
	NotInRangeComplainer()
}

NotInRangeComplainer represents the error situation where a rune that represents a numeric value is not in the range of the Go numeric type it is trying to be converted to.

For example, although '¼' is numeric it cannot be converted to an int64 since int64 cannot represent fractional numbers.

Example
r := '½'

i64, err := numeric.Int64(r)
if nil != err {
	switch err.(type) {
	case numeric.NotInRangeComplainer:
		fmt.Printf("Although %q is numeric, it is not in the range of an int64.\n", r)
		return
	default:
		fmt.Println("Some other error.")
		return
	}
}

fmt.Printf("The int64 number is: %d.\n", i64)
Output:

Although '½' is numeric, it is not in the range of an int64.

type NotNumericComplainer

type NotNumericComplainer interface {
	error
	NotNumericComplainer()
}

NotNumericComplainer represents the error situation where a rune that does not represent a numeric value tries to be converted to a Go numeric type.

For example, '&' cannot be converted to an int64 since it does not represent a numeric type.

Example
r := '&'

i64, err := numeric.Int64(r)
if nil != err {
	switch err.(type) {
	case numeric.NotNumericComplainer:
		fmt.Printf("The rune %q is NOT numeric.\n", r)
		return
	default:
		fmt.Println("Some other error.")
		return
	}
}

fmt.Printf("The int64 number is: %d.\n", i64)
Output:

The rune '&' is NOT numeric.

Jump to

Keyboard shortcuts

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