Skip to content

Commit

Permalink
Handling Arabic Hundreds
Browse files Browse the repository at this point in the history
  • Loading branch information
yousifnimah committed Jul 23, 2023
1 parent cc6fd9f commit 00ee51a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
99 changes: 76 additions & 23 deletions NumToWords/EntryAr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,84 @@ type EntryAr struct {
}

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 := ""

switch {
case Input < 10: //0-9
{
Result = Entry.handleUnits(Input)
}
case Input < 20: //10-19
{
Result = Entry.handleTeens(Input)
}
case Input < 100: //20-99
{
Result = Entry.handleTwenties(Input)
}
case Input < 1000: //100-999
{
Result = Entry.handleHundreds(Input)
}
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))
//case Input < 100000: //1000-9999
// {
// Result = Entry.handleTeens(Input)
// }
}
return strings.TrimSpace(result)

//if Input < 10 {
// result = Entry.handleUnits(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 (EntAr EntryAr) handleUnits(Input int) string {
return EntAr.LocalizedEntity.Units[Input]
}

func (EntAr EntryAr) handleTeens(Input int) string {
return EntAr.LocalizedEntity.Teens[Input-10]
}

func (EntAr EntryAr) handleTwenties(Input int) string {
And := ""
Seg01 := EntAr.Translate(Input % 10)
Seg02 := EntAr.LocalizedEntity.Tens[Input/10]
if Seg01 != "" && Seg02 != "" {
And = EntAr.LocalizedEntity.And
}
return fmt.Sprintf("%s %s %s", Seg01, And, Seg02)
}

func (EntAr EntryAr) handleHundreds(Input int) string {
And := ""
Seg01 := EntAr.LocalizedEntity.Hundreds[(Input / 100)]
//Seg02 := EntAr.LocalizedEntity.Hundred
Seg03 := EntAr.Translate(Input % 100)
if Seg01 != "" && Seg03 != "" {
And = EntAr.LocalizedEntity.And
}

return fmt.Sprintf("%s %s %s", Seg01, And, Seg03)
}

func (Entry EntryAr) ZeroResponse() string {
Expand Down
1 change: 1 addition & 0 deletions NumToWords/locales/Lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Lang struct {
Units []string
Teens []string
Tens []string
Hundreds []string
And string
Zero string
Hundred string
Expand Down
1 change: 1 addition & 0 deletions NumToWords/locales/ar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var AR = Lang{
Units: []string{"", "واحد", "اثنان", "ثلاثة", "أربعة", "خمسة", "ستة", "سبعة", "ثمانية", "تسعة"},
Teens: []string{"عشرة", "أحد عشر", "اثنا عشر", "ثلاثة عشر", "أربعة عشر", "خمسة عشر", "ستة عشر", "سبعة عشر", "ثمانية عشر", "تسعة عشر"},
Tens: []string{"", "", "عشرون", "ثلاثون", "أربعون", "خمسون", "ستون", "سبعون", "ثمانون", "تسعون"},
Hundreds: []string{"", "مئة", "مئتان", "ثلاث مئة", "اربع مئة", "خمس مئة", "ست مئة", "سبع مئة", "ثمان مئة", "تسع مئة"},
And: "و",
Zero: "صفر",
Hundred: "مئة",
Expand Down
2 changes: 1 addition & 1 deletion _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func main() {
input := 32
input := 656
words, err := NumToWords.Convert(input, "ar")
if err != nil {
return
Expand Down

0 comments on commit 00ee51a

Please sign in to comment.