Skip to content

Commit

Permalink
New language: spanish #12
Browse files Browse the repository at this point in the history
  • Loading branch information
ehx committed Oct 5, 2016
1 parent ad33fda commit 2f8dee7
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
80 changes: 80 additions & 0 deletions es.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ntw

import (
"fmt"
"strings"
)

var spanishMegas = []string{"", "mil", "millon", "billon", "trillon", "cuatrillon", "quintillon", "sextillon", "septillon", "octillon", "nonillon", "decillon", "oncillon", "docillon", "trecillon", "catorcillon"}
var spanishUnits = []string{"", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve"}
var spanishTens = []string{"", "diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa"}
var spanishTeens = []string{"diez", "once", "doce", "trece", "catorce", "quince", "dieciseis", "diecisiete", "dieciocho", "diecinueve"}

// IntegerToSpanish converts an integer to spanish words
func IntegerToSpanish(input int) string {
//log.Printf("Input: %d\n", input)
words := []string{}

if input < 0 {
words = append(words, "menos")
input *= -1
}

// split integer in triplets
triplets := integerToTriplets(input)
//log.Printf("Triplets: %v\n", triplets)

// zero is a special case
if len(triplets) == 0 {
return "zero"
}

// iterate over triplets
for idx := len(triplets) - 1; idx >= 0; idx-- {
triplet := triplets[idx]
//log.Printf("Triplet: %d (idx=%d)\n", triplet, idx)

// nothing todo for empty triplet
if triplet == 0 {
continue
}

// three-digits
hundreds := triplet / 100 % 10
tens := triplet / 10 % 10
units := triplet % 10
//log.Printf("Hundreds:%d, Tens:%d, Units:%d\n", hundreds, tens, units)
if hundreds > 0 {
words = append(words, spanishUnits[hundreds], "mil")
}

if tens == 0 && units == 0 {
goto tripletEnd
}

switch tens {
case 0:
words = append(words, spanishUnits[units])
case 1:
words = append(words, spanishTeens[units])
break
default:
if units > 0 {
word := fmt.Sprintf("%s-%s", spanishTens[tens], spanishUnits[units])
words = append(words, word)
} else {
words = append(words, spanishTens[tens])
}
break
}

tripletEnd:
// mega
if mega := spanishMegas[idx]; mega != "" {
words = append(words, mega)
}
}

//log.Printf("Words length: %d\n", len(words))
return strings.Join(words, " ")
}
72 changes: 72 additions & 0 deletions es_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ntw

import (
"fmt"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func ExampleIntegerToSpanish() {
fmt.Println(IntegerToSpanish(42))
// Output: forty-two
}

func TestIntegerToSpanish(t *testing.T) {
Convey("Testing IntegerToSpanish()", t, FailureContinues, func() {
testing := map[int]string{
0: "cero",
1: "uno",
9: "nueve",
10: "diez",
11: "once",
19: "diecinueve",
20: "veinte",
21: "veintiuno",
80: "ochenta",
90: "noventa",
99: "noventa y nueve",
100: "cien",
101: "ciento uno",
111: "ciento once",
120: "ciento veinte",
121: "ciento veinte y uno",
900: "novecientos",
909: "novecientos nueve",
919: "novecientos diecinueve",
990: "novecientos noventa",
999: "novecientos noventa y nueve",
1000: "mil",
2000: "dos mil",
4000: "cuatro mil",
5000: "cinco mil",
11000: "once mil",
21000: "veintiún mil",
999000: "novecientos noventa y nueve mil",
999999: "Novecientos noventa y nueve mil novecientos noventa y nueve",
1000000: "un millon",
2000000: "dos millon",
4000000: "cuatro millones",
5000000: "cinco millones",
100100100: "cien millones cien mil cien",
500500500: "quinientos millones quinientos mil quinientos",
606606606: "seiscientos seis millones seiscientos seis mil seiscientos seis",
999000000: "novecientos noventa y nueve millones",
999000999: "novecientos noventa y nueve millones novecientos noventa y nueve",
999999000: "novecientos noventa y nueve millones novecientos noventa y nueve mil",
999999999: "novecientos noventa y nueve millones novecientos noventa y nueve mil novecientos noventa y nueve",
1174315110: "mil ciento setenta y cuatro millones trescientos quince mil ciento diez",
1174315119: "mil ciento setenta y cuatro millones trescientos quince mil ciento diecinueve",
15174315119: "quince mil ciento setenta y cuatro millones trescientos quince mil ciento diecinueve",
35174315119: "treinta y cinco mil ciento setenta y cuatro millones trescientos quince mil ciento diecinueve",
935174315119: "novecientos treinta y cinco mil ciento setenta y cuatro millones trescientos quince mil ciento diecinueve",
2935174315119: "dos billones novecientos treinta y cinco mil ciento setenta y cuatro millones trescientos quince mil ciento diecinueve",
}
for input, expectedOutput := range testing {
So(IntegerToSpanish(input), ShouldEqual, expectedOutput)
}

// testing negative values
So(IntegerToSpanish(-1), ShouldEqual, "menos uno")
})
}

0 comments on commit 2f8dee7

Please sign in to comment.