Adding library code and tests
This commit is contained in:
parent
a26bc1e96c
commit
d22014d337
19
README.md
Normal file
19
README.md
Normal file
@ -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)
|
||||
```
|
103
numericword.go
Normal file
103
numericword.go
Normal file
@ -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 <tony@grosinger.net>
|
||||
*/
|
||||
|
||||
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)
|
||||
}
|
40
numericword_test.go
Normal file
40
numericword_test.go
Normal file
@ -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")
|
||||
}
|
Reference in New Issue
Block a user