-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, " ")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "منفی یک") | ||
}) | ||
} |