From d22014d33760348e23697193c50ef9c10199c918 Mon Sep 17 00:00:00 2001 From: Tony Grosinger Date: Sun, 1 Feb 2015 21:38:53 -0800 Subject: [PATCH] Adding library code and tests --- README.md | 19 ++++++++ numericword.go | 103 ++++++++++++++++++++++++++++++++++++++++++++ numericword_test.go | 40 +++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 README.md create mode 100644 numericword.go create mode 100644 numericword_test.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..2270425 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Numeric Word + +Simple library to convert a number from int64 to a word representation. +Currently supports any int64 value. + +## Installation + +``` +$ go get github.com/tgrosinger/numericword +``` + +## Usage + +``` +import "github.com/tgrosinger/numericword" + +number := int64(12345) +numerString := numericword.ToWord(number) +``` diff --git a/numericword.go b/numericword.go new file mode 100644 index 0000000..aa7efe2 --- /dev/null +++ b/numericword.go @@ -0,0 +1,103 @@ +package numericword + +/* + numericword converts a number into equivalent words. + + ex: 1024 -> one thousand twenty four + -534 -> negative five hundred thirty four + + Author: Tony Grosinger +*/ + +import ( + "fmt" +) + +var ( + bigkeys = []int64{ + 1000000000000000000, + 1000000000000000, + 1000000000000, + 1000000000, + 1000000, + 1000, + 100} + + bignames = map[int64]string{ + 1000000000000000000: "quintillion", + 1000000000000000: "quadrillion", + 1000000000000: "trillion", + 1000000000: "billion", + 1000000: "million", + 1000: "thousand", + 100: "hundred"} + + tens = []string{ + "", + "", + "twenty", + "thirty", + "forty", + "fifty", + "sixty", + "seventy", + "eighty", + "ninety"} + + ones = []string{ + "zero", + "one", + "two", + "three", + "four", + "five", + "six", + "seven", + "eight", + "nine", + "ten", + "eleven", + "twelve", + "thirteen", + "fourteen", + "fifteen", + "sixteen", + "seventeen", + "eighteen", + "nineteen"} +) + +func convert(i int64) string { + if i < 20 { + return ones[i] + } else if i < 100 && i%10 == 0 { + return tens[i/10] + } else if i < 100 { + return fmt.Sprintf("%s %s", tens[i/10], convert(i%10)) + } + + for j := 0; j < len(bigkeys); j++ { + place := bigkeys[j] + if i >= place { + if i%place == 0 { + // Skip the extra zero that would occur + return fmt.Sprintf("%s %s", convert(i/place), bignames[place]) + } else { + return fmt.Sprintf("%s %s %s", + convert(i/place), + bignames[place], + convert(i%place)) + } + } + } + + return "oops" +} + +// ToWords converts a number to words up to max or min size of int64 +func ToWords(i int64) string { + if i < 0 { + return fmt.Sprintf("negative %s", convert(-1*i)) + } + return convert(i) +} diff --git a/numericword_test.go b/numericword_test.go new file mode 100644 index 0000000..4138de8 --- /dev/null +++ b/numericword_test.go @@ -0,0 +1,40 @@ +package numericword + +import ( + "testing" +) + +func verify(t *testing.T, i int64, s string) { + res := ToWords(i) + if res != s { + t.Errorf("Converting %d\nExpected '%s'\nReceived '%s'", i, s, res) + } +} + +func TestSmallPositiveValues(t *testing.T) { + verify(t, 0, "zero") + verify(t, 10, "ten") + verify(t, 55, "fifty five") + verify(t, 100, "one hundred") + verify(t, 101, "one hundred one") + verify(t, 111, "one hundred eleven") + verify(t, 859, "eight hundred fifty nine") +} + +func TestLargePositiveValues(t *testing.T) { + verify(t, 1000, "one thousand") + verify(t, 1001, "one thousand one") + verify(t, 1141, "one thousand one hundred forty one") + verify(t, 9400, "nine thousand four hundred") + verify(t, 900001, "nine hundred thousand one") + verify(t, 34058689, "thirty four million fifty eight thousand six hundred eighty nine") +} + +func TestNegativeValues(t *testing.T) { + verify(t, -10, "negative ten") + verify(t, -55, "negative fifty five") + verify(t, -100, "negative one hundred") + verify(t, -101, "negative one hundred one") + verify(t, -111, "negative one hundred eleven") + verify(t, -859, "negative eight hundred fifty nine") +}