Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Swedish language #174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ gin-bin

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.idea/
114 changes: 61 additions & 53 deletions sv-se.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ntw

import (
"fmt"
"math"
"strings"
)

Expand All @@ -16,74 +16,82 @@ func init() {
}
}

var (
swedishMegas = []string{"tusen", "miljon", "miljard", "biljon", "kvadriljon", "kvintiljon"} //"sextillion", "septillion", "octillion", "nonillion", "decillion"
swedishUnits = []string{"noll", "en", "två", "tre", "fyra", "fem", "sex", "sju", "åtta", "nio", "tio", "elva", "tolv", "tretton", "fjorton", "femton", "sexton", "sjutton", "arton", "nitton"}
swedishTens = []string{"", "", "tjugo", "trettio", "fyrtio", "femtio", "sextio", "sjuttio", "åttio", "nittio"}
)

// IntegerToSvSe converts an integer to Swedish words
func IntegerToSvSe(input int) string {
var swedishMegas = []string{"", "tusen", "miljoner", "miljarder", "biljoner", "kvadriljon", "kvintiljon", "sextillion", "septillion", "octillion", "nonillion", "decillion"}
var swedishUnits = []string{"noll", "en", "två", "tre", "fyra", "fem", "sex", "sju", "åtta", "nio"}
var swedishTens = []string{"noll", "tio", "tjugo", "trettio", "fyrtio", "femtio", "sextio", "sjuttio", "åttio", "nittio"}
var swedishTeens = []string{"tio", "elva", "tolv", "tretton", "fjorton", "femton", "sexton", "sjutton", "arton", "nitton"}
func IntegerToSvSe(n int) string {
result := integerToSvSe(n, true, true)
result = strings.Replace(result, "etttusen", "ettusen", -1)

words := []string{}
return strings.TrimSpace(result)
}

func integerToSvSe(input int, firstCall bool, isSuffix bool) string {

if input < 0 {
words = append(words, "mindre")
input *= -1
return "mindre " + integerToSvSe(-input, true, isSuffix)
}

// split integer in triplets
triplets := integerToTriplets(input)

// zero is a special case
if len(triplets) == 0 {
if input == 0 && firstCall {
return "noll"
} else if input == 0 {
return ""
}

// iterate over triplets
for idx := len(triplets) - 1; idx >= 0; idx-- {
triplet := triplets[idx]
// nothing todo for empty triplet
if triplet == 0 {
continue
}
if input < 20 {
return addGroupSvSe(input, isSuffix)
}

// three-digits
hundreds := triplet / 100 % 10
tens := triplet / 10 % 10
units := triplet % 10
switch hundreds {
case 0:
break
default:
words = append(words, swedishUnits[hundreds], "hundra")
break
}
if input < 100 {
return swedishTens[input/10] + addGroupSvSe(input%10, isSuffix)
}

if tens == 0 && units == 0 {
goto tripletEnd
if input < 1000 {
if input/100 == 1 {
return "etthundra" + addGroupSvSe(input%100, isSuffix)
}
return addGroupSvSe(input/100, false) + "hundra" + addGroupSvSe(input%100, isSuffix)
}

switch tens {
case 0:
words = append(words, swedishUnits[units])
case 1:
words = append(words, swedishTeens[units])
break
default:
if units > 0 {
word := fmt.Sprintf("%s-%s", swedishTens[tens], swedishUnits[units])
words = append(words, word)
} else {
words = append(words, swedishTens[tens])
for i := len(swedishMegas) - 1; i >= 0; i-- {
if power := int(math.Pow(1000, float64(i+1))); input > power-1 {
mega := swedishMegas[i]

if power > 1000 {
if input/power > 1 {
mega += "er"
}
mega = " " + mega + " "
return integerToSvSe(input/power, false, isSuffix) + mega + integerToSvSe(input%power, false, isSuffix)
}
break
}

tripletEnd:
// mega
if mega := swedishMegas[idx]; mega != "" {
words = append(words, mega)
return integerToSvSe(input/power, false, false) + mega + integerToSvSe(input%power, false, isSuffix)
}
}
return ""
}

func addGroupSvSe(n int, isSuffix bool) string {
if n == 0 {
return ""
}
if !isSuffix && n == 1 {
return "ett"
}
if n < 20 {
return swedishUnits[n]
}
if n%10 == 0 {
return swedishTens[n/10]
}

if !isSuffix && n%10 == 1 {
return swedishTens[n/10] + "ett"
}

return strings.Join(words, " ")
return swedishTens[n/10] + swedishUnits[n%10]
}
80 changes: 45 additions & 35 deletions sv-se_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ package ntw

import (
"fmt"
"math"
"testing"

"github.com/stretchr/testify/assert"
)

func ExampleIntegerToSvSe() {
fmt.Println(IntegerToSvSe(42))
// Output: fyrtio-två
// Output: fyrtiotvå
}

func TestIntegerToSvSe(t *testing.T) {
t.Parallel()

tests := map[int]string{
-1241521: "mindre en miljon tvåhundrafyrtioettusenfemhundratjugoen",
-200: "mindre tvåhundra",
-1: "mindre en",
0: "noll",
1: "en",
Expand All @@ -24,45 +27,52 @@ func TestIntegerToSvSe(t *testing.T) {
11: "elva",
19: "nitton",
20: "tjugo",
21: "tjugo-en",
21: "tjugoen",
80: "åttio",
90: "nittio",
99: "nittio-nio",
100: "en hundra",
101: "en hundra en",
111: "en hundra elva",
120: "en hundra tjugo",
121: "en hundra tjugo-en",
900: "nio hundra",
909: "nio hundra nio",
919: "nio hundra nitton",
990: "nio hundra nittio",
999: "nio hundra nittio-nio",
1000: "en tusen",
2000: "två tusen",
4000: "fyra tusen",
5000: "fem tusen",
11000: "elva tusen",
21000: "tjugo-en tusen",
999000: "nio hundra nittio-nio tusen",
999999: "nio hundra nittio-nio tusen nio hundra nittio-nio",
1000000: "en miljoner",
99: "nittionio",
100: "etthundra",
101: "etthundraen",
111: "etthundraelva",
120: "etthundratjugo",
121: "etthundratjugoen",
900: "niohundra",
909: "niohundranio",
919: "niohundranitton",
990: "niohundranittio",
999: "niohundranittionio",
1000: "ettusen",
1500: "ettusenfemhundra",
2000: "tvåtusen",
4000: "fyratusen",
5000: "femtusen",
11000: "elvatusen",
21000: "tjugoettusen",
999000: "niohundranittioniotusen",
999999: "niohundranittioniotusenniohundranittionio",
1000000: "en miljon",
2000000: "två miljoner",
4000000: "fyra miljoner",
5000000: "fem miljoner",
100100100: "en hundra miljoner en hundra tusen en hundra",
500500500: "fem hundra miljoner fem hundra tusen fem hundra",
606606606: "sex hundra sex miljoner sex hundra sex tusen sex hundra sex",
999000000: "nio hundra nittio-nio miljoner",
999000999: "nio hundra nittio-nio miljoner nio hundra nittio-nio",
999999000: "nio hundra nittio-nio miljoner nio hundra nittio-nio tusen",
999999999: "nio hundra nittio-nio miljoner nio hundra nittio-nio tusen nio hundra nittio-nio",
1174315110: "en miljarder en hundra sjuttio-fyra miljoner tre hundra femton tusen en hundra tio",
1174315119: "en miljarder en hundra sjuttio-fyra miljoner tre hundra femton tusen en hundra nitton",
15174315119: "femton miljarder en hundra sjuttio-fyra miljoner tre hundra femton tusen en hundra nitton",
35174315119: "trettio-fem miljarder en hundra sjuttio-fyra miljoner tre hundra femton tusen en hundra nitton",
935174315119: "nio hundra trettio-fem miljarder en hundra sjuttio-fyra miljoner tre hundra femton tusen en hundra nitton",
2935174315119: "två biljoner nio hundra trettio-fem miljarder en hundra sjuttio-fyra miljoner tre hundra femton tusen en hundra nitton",
100100100: "etthundra miljoner etthundratusenetthundra",
500500500: "femhundra miljoner femhundratusenfemhundra",
606606606: "sexhundrasex miljoner sexhundrasextusensexhundrasex",
999000000: "niohundranittionio miljoner",
999000999: "niohundranittionio miljoner niohundranittionio",
999999000: "niohundranittionio miljoner niohundranittioniotusen",
999999999: "niohundranittionio miljoner niohundranittioniotusenniohundranittionio",
1001000000: "en miljard en miljon",
1002000000: "en miljard två miljoner",
1111111111: "en miljard etthundraelva miljoner etthundraelvatusenetthundraelva",
1010101010: "en miljard tio miljoner etthundraettusentio",
1174315110: "en miljard etthundrasjuttiofyra miljoner trehundrafemtontusenetthundratio",
1174315119: "en miljard etthundrasjuttiofyra miljoner trehundrafemtontusenetthundranitton",
10101010101: "tio miljarder etthundraen miljoner tiotusenetthundraen",
15174315119: "femton miljarder etthundrasjuttiofyra miljoner trehundrafemtontusenetthundranitton",
35174315119: "trettiofem miljarder etthundrasjuttiofyra miljoner trehundrafemtontusenetthundranitton",
935174315119: "niohundratrettiofem miljarder etthundrasjuttiofyra miljoner trehundrafemtontusenetthundranitton",
2935174315119: "två biljoner niohundratrettiofem miljarder etthundrasjuttiofyra miljoner trehundrafemtontusenetthundranitton",
math.MaxInt64: "nio kvintiljoner tvåhundratjugotre kvadriljoner trehundrasjuttiotvå biljoner trettiosex miljarder åttahundrafemtiofyra miljoner sjuhundrasjuttiofemtusenåttahundrasju",
}

for input, expectedOutput := range tests {
Expand Down