diff --git a/NumToWords/Convert.go b/NumToWords/Convert.go index dfe2c99..385f8e7 100644 --- a/NumToWords/Convert.go +++ b/NumToWords/Convert.go @@ -2,6 +2,7 @@ package NumToWords import ( "errors" + "fmt" ) type NumToWord struct{} @@ -25,6 +26,7 @@ func Convert(Input int, Language string) (string, error) { Ent.BootstrapLanguage() if IsZero(Input) { + fmt.Println("Input") return Ent.ZeroResponse(), nil } diff --git a/NumToWords/EntryAr.go b/NumToWords/EntryAr.go index f9abe49..9c7d3c8 100644 --- a/NumToWords/EntryAr.go +++ b/NumToWords/EntryAr.go @@ -1,15 +1,42 @@ package NumToWords -import "github.com/yousifnimah/NumToWordsGo/NumToWords/locales" +import ( + "fmt" + "github.com/yousifnimah/NumToWordsGo/NumToWords/locales" + "strings" +) type EntryAr struct { Language string LocalizedEntity locales.Lang } -func (Entry *EntryAr) Translate(i int) string { - //TODO implement me - panic("implement me") +func (Entry *EntryAr) Translate(Input int) string { + And := "" + result := "" + if Input < 10 { + result = Entry.LocalizedEntity.Units[Input] + } else if Input < 20 { + result = Entry.LocalizedEntity.Teens[Input-10] + } else if Input < 100 { + Seg01 := Entry.Translate(Input % 10) + Seg02 := Entry.LocalizedEntity.Tens[Input/10] + if Seg01 != "" && Seg02 != "" { + And = Entry.LocalizedEntity.And + } + result = fmt.Sprintf("%s %s %s", Seg01, And, Seg02) + } else if Input < 1000 { + result = fmt.Sprintf("%s %s %s", Entry.Translate(Input/100), Entry.LocalizedEntity.Hundred, Entry.Translate(Input%100)) + } else if Input < 1000000 { + result = fmt.Sprintf("%s %s %s", Entry.Translate(Input/1000), Entry.LocalizedEntity.Thousand, Entry.Translate(Input%1000)) + } else if Input < 1000000000 { + result = fmt.Sprintf("%s %s %s", Entry.Translate(Input/1000000), Entry.LocalizedEntity.Million, Entry.Translate(Input%1000000)) + } else if Input < 10000000000 { + result = fmt.Sprintf("%s %s %s", Entry.Translate(Input/1000000000), Entry.LocalizedEntity.Billion, Entry.Translate(Input%1000000000)) + } else { + result = fmt.Sprintf("%s %s %s", Entry.Translate(Input/1000000000000), Entry.LocalizedEntity.Trillion, Entry.Translate(Input%1000000000000)) + } + return strings.TrimSpace(result) } func (Entry EntryAr) ZeroResponse() string { diff --git a/NumToWords/locales/Lang.go b/NumToWords/locales/Lang.go index b6d0117..65d9624 100644 --- a/NumToWords/locales/Lang.go +++ b/NumToWords/locales/Lang.go @@ -4,6 +4,7 @@ type Lang struct { Units []string Teens []string Tens []string + And string Zero string Hundred string Thousand string diff --git a/NumToWords/locales/ar.go b/NumToWords/locales/ar.go index 9a27f5a..e836b78 100644 --- a/NumToWords/locales/ar.go +++ b/NumToWords/locales/ar.go @@ -1,9 +1,10 @@ package locales var AR = Lang{ - Units: []string{"صفر", "واحد", "اثنان", "ثلاثة", "أربعة", "خمسة", "ستة", "سبعة", "ثمانية", "تسعة"}, + Units: []string{"", "واحد", "اثنان", "ثلاثة", "أربعة", "خمسة", "ستة", "سبعة", "ثمانية", "تسعة"}, Teens: []string{"عشرة", "أحد عشر", "اثنا عشر", "ثلاثة عشر", "أربعة عشر", "خمسة عشر", "ستة عشر", "سبعة عشر", "ثمانية عشر", "تسعة عشر"}, Tens: []string{"", "", "عشرون", "ثلاثون", "أربعون", "خمسون", "ستون", "سبعون", "ثمانون", "تسعون"}, + And: "و", Zero: "صفر", Hundred: "مئة", Thousand: "الف", diff --git a/NumToWords/locales/en.go b/NumToWords/locales/en.go index 92d0e4b..15eac73 100644 --- a/NumToWords/locales/en.go +++ b/NumToWords/locales/en.go @@ -4,6 +4,7 @@ var EN = Lang{ Units: []string{"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}, Teens: []string{"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}, Tens: []string{"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}, + And: "", Zero: "zero", Hundred: "hundred", Thousand: "thousand", diff --git a/_example/main.go b/_example/main.go index 9f87dab..5d9d33e 100644 --- a/_example/main.go +++ b/_example/main.go @@ -6,8 +6,8 @@ import ( ) func main() { - input := 1000000000000 - words, err := NumToWords.Convert(input, "en") + input := 32 + words, err := NumToWords.Convert(input, "ar") if err != nil { return }