Skip to content

Commit

Permalink
Merge 520e727 into 1b7abb7
Browse files Browse the repository at this point in the history
  • Loading branch information
omidnikrah committed Aug 11, 2019
2 parents 1b7abb7 + 520e727 commit 6e5810a
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Convert numbers to words.
* Portuguese / Portugal [pt-pt] 🇵🇹
* Polish / Poland [pl-pl] 🇵🇱
* Russian / Russia [ru-ru] 🇷🇺
* Iranian / Iran [ir-ir] 🇮🇷

* Roman Numbers
* Roman Numbers (with Unicode) Ⅷ
Expand Down Expand Up @@ -67,6 +68,9 @@ $ number-to-words --lang=roman-unicode

$ number-to-words --lang=aegean 42
𐄓𐄈

$ number-to-words --lang=ir 42
چهل و دو
```

default language is english
Expand All @@ -91,6 +95,7 @@ kırk iki
quarenta e dois
XLII
𐄓𐄈
چهل و دو

$ number-to-words --lang=all 1
one
Expand All @@ -104,6 +109,7 @@ bir
um
I
𐄇
یک

$ number-to-words --lang=all 1337
one thousand three hundred thirty-seven
Expand All @@ -117,6 +123,7 @@ bin üç yüz otuz yedi
mil trezentos e trinta e sete
MCCCXXXVII
𐄢𐄛𐄒𐄍
یک هزار سیصد سی و هفت

$ number-to-words --lang=all 1234567890
one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety
Expand All @@ -128,6 +135,7 @@ en miljarder två hundra trettio-fyra miljoner fem hundra sextio-sju tusen åtta
één miljard tweehonderdvierendertig miljoen vijfhonderdzevenenzestigduizend achthonderdnegentig
bir milyar iki yüz otuz dört milyon beş yüz altmış yedi bin sekiz yüz doksan
mil milhões duzentos e trinta e quatro milhões quinhentos e sessenta e sete mil oitocentos e noventa
یک میلیارد دویست سی و چهار میلیون پانصد شصد و هفت هزار هشتصد نود

$ number-to-words --lang=all 1000000000000
one trillion
Expand Down Expand Up @@ -176,6 +184,7 @@ AVAILABLE LANGUAGES:
Dutch (nl, dutch, nl-nl, nl_NL) 🇳🇱
Portuguese (Portugal) (pt, pt-pt, pt_PT, portuguese) 🇵🇹
Polish (Poland) (pl, pl-pl, pl_PL, polish) 🇵🇱
Iranian (Iran) (ir, ir-ir, ir_IR, Iran) 🇮🇷
Roman Numbers (roman)
```

Expand Down Expand Up @@ -252,6 +261,9 @@ fmt.Println(ntw.IntegerToRomanUnicode(42)) // roman (unicode)

fmt.Println(ntw.IntegerToAegean(42)) // aegean (unicode)
// Outputs: 𐄓𐄈

fmt.Println(ntw.IntegerToIrIr(42)) // iranian
// Outputs: چهل و دو
```

## Install
Expand Down
87 changes: 87 additions & 0 deletions ir-ir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ntw

import (
"fmt"
"strings"
)

func init() {
// register the language
Languages["ir-ir"] = Language{
Name: "Iranian",
Aliases: []string{"ir", "ir-ir", "ir_IR", "iranian"},
Flag: "🇮🇷",

IntegerToWords: IntegerToIrIr,
}
}

func IntegerToIrIr(input int) string {
var iranianMegas = []string{"", "هزار", "میلیون", "میلیارد", "بیلیون", "بیلیارد", "تریلیون", "تریلیارد"}
var iranianUnits = []string{"", "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه"}
var iranianTens = []string{"", "ده", "بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود"}
var iranianTeens = []string{"ده", "یازده", "دوازده", "سیزده", "چهارده", "پانزده", "شانزده", "هفده", "هجده", "نوزده"}
var iranianHundreds = []string{"", "صد", "دویست", "سیصد", "چهارصد", "پانصد", "ششصد", "هفتصد", "هشتصد", "نهصد"}

//log.Printf("Input: %d\n", input)
words := []string{}

if input < 0 {
words = append(words, "منفی")
input *= -1
}

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

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

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

if triplet == 0 {
continue
}

// three-digits
hundreds := triplet / 100 % 10
tens := triplet / 10 % 10
units := triplet % 10

if hundreds > 0 {
words = append(words, iranianHundreds[hundreds])
}

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

switch tens {
case 0:
words = append(words, iranianUnits[units])
case 1:
words = append(words, iranianTeens[units])
break
default:
if units > 0 {
word := fmt.Sprintf("%s و %s", iranianTens[tens], iranianUnits[units])
words = append(words, word)
} else {
words = append(words, iranianTens[tens])
}
break
}

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

return strings.TrimSpace(strings.Join(words, " "))
}
66 changes: 66 additions & 0 deletions ir-ir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ntw

import (
"fmt"
"testing"

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

func ExampleIntegerToIrIr() {
fmt.Println(IntegerToIrIr(42))
// Output: forty-two
}

func TestIntegerToIrIr(t *testing.T) {
Convey("Testing IntegerToIrIr()", t, FailureContinues, func() {
testing := map[int]string{
0: "صفر",
1: "یک",
9: "نه",
10: "ده",
11: "یازده",
19: "نوزده",
20: "بیست",
21: "بیست و یک",
80: "هشتاد",
90: "نود",
99: "نود و نه",
100: "صد",
101: "صد یک",
111: "صد یازده",
120: "صد بیست",
121: "صد بیست و یک",
900: "نهصد",
909: "نهصد نه",
919: "نهصد نوزده",
990: "نهصد نود",
999: "نهصد نود و نه",
1000: "یک هزار",
2000: "دو هزار",
4000: "چهار هزار",
5000: "پنج هزار",
11000: "بازده هزار",
21000: "بیست و یک هزار",
999000: "نهصد نود و نه هزار",
999999: "نهصد نود و نه هزار نهصد نود و نه",
1000000: "یک میلیون",
2000000: "دو میلیون",
4000000: "چهار میلیون",
5000000: "پنج میلیون",
100100100: "صد میلیون صد هزار صد",
500500500: "پانصد میلیون پانصد هزار پانصد",
606606606: "ششصد شش میلیون ششصد شش هزار ششصد شش",
999000000: "نهصد نود و نه میلیون",
999000999: "نهصد نود و نه میلیون نهصد نود و نه",
999999000: "نهصد نود و نه میلیون نهصد نود و نه هزار",
999999999: "نهصد نود و نه میلیون نهصد نود و نه هزار نهصد نود و نه",
}
for input, expectedOutput := range testing {
So(IntegerToIrIr(input), ShouldEqual, expectedOutput)
}

// testing negative values
So(IntegerToIrIr(-1), ShouldEqual, "منفی یک")
})
}

0 comments on commit 6e5810a

Please sign in to comment.