Skip to content

Commit

Permalink
Added Indian conversion (and subcontinent) for currencies (#155)
Browse files Browse the repository at this point in the history
* Indian currency conversion (also for the subcontinent)

* updated readme

* Unwanted log commented out

* Fixes to comments and added info about indian language
  • Loading branch information
chilarai committed Oct 12, 2022
1 parent 9664a0a commit bb890e9
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@

## Supported languages



| Code | Flag | Language | Main Region | 42 |
| --------------- | ---- | -------------------------------- | ----------- | ---------------- |
| `en`, `en-us` | 🇺🇸 | American English | USA | forty-two |
| `fr`, `fr-fr` | 🇫🇷 | French, Français | France | quarante-deux |
| `it`, `it-it` | 🇮🇹 | Italiano | Italy | quarantadue |
| `in`, `en-in` | 🇮🇳 | Indian English | India | forty-two |
| `es`, `es-es` | 🇪🇸 | European Spanish | Spain | cuarenta y dos |
| `dk`, `da-dk` | 🇩🇰 | Danish | Denmark | toogfyrre |
| `dk`, `da-dk` | 🇩🇰 | Danish | Denmark | toogfyrre |
| `se`, `sv-se` | 🇸🇪 | Swedish | Sweden | fyrtio-två |
| `nl`, `nl-nl` | 🇳🇱 | Dutch | Netherlands | tweeenveertig |
| `tr`, `tr-tr` | 🇹🇷 | Turkish | Turkey | kırk iki |
Expand Down Expand Up @@ -50,6 +49,9 @@ nonante-deux
$ number-to-words --lang=it 42
quarantadue

$ number-to-words --lang=in 42
forty-two

$ number-to-words --lang=es 42
cuarenta y dos

Expand Down Expand Up @@ -149,6 +151,7 @@ MCCCXXXVII
seribu tiga ratus tiga puluh tujuh

$ number-to-words --lang=all 1234567890
one arab twenty-three crore forty-five lakh sixty-seven thousand eight hundred ninety
one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety
un milliard deux cent trente-quatre millions cinq cent soixante-sept mille huit cent quatre-vingt-dix
un milliard deux cent trente-quatre millions cinq cent soixante-sept mille huit cent nonante
Expand Down Expand Up @@ -204,6 +207,7 @@ AVAILABLE LANGUAGES:
Belgian French (fr-be, fr_BE, belgian) 🇧🇪
French (fr, fr-fr, fr_FR, french) 🇫🇷
Italian (it, it-it, it_IT, italian) 🇮🇹
Indian English (in, en-in, indian) 🇮🇳
Roman Numbers (with Unicode) (roman-unicode)
Danish (da-dk, da_DK, danish) 🇩🇰
Swedish (sv-se, sv_SE, swedish) 🇸🇪
Expand Down
94 changes: 94 additions & 0 deletions en-in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package ntw

import (
"fmt"
"strings"
)

func init() {
// register the language
Languages["en-in"] = Language{
Name: "Indian English",
Aliases: []string{"en", "en-in", "indian", "english"},
Flag: "🇮🇳",

IntegerToWords: IntegerToEnIn,
}
}

// IntegerToEnIn converts an integer to Indian English words
func IntegerToEnIn(input int) string {
var indianMegas = []string{"", "thousand", "lakh", "crore", "arab", "kharab", "neel", "padma", "shankh", "mahashankh"}
var indianUnits = []string{"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
var indianTens = []string{"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
var indianTeens = []string{"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}

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

if input < 0 {
words = append(words, "minus")
input *= -1
}

// split integer in hybrids
var hybrids []int
hybrids = integerToDHybrid(input)


// log.Printf("Hybrids: %v\n", hybrids)

// zero is a special case
if len(hybrids) == 0 {
return "zero"
}

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

// nothing todo for empty hybrid
if hybrid == 0 {
continue
}

// three-digits
hundreds := hybrid / 100 % 10
tens := hybrid / 10 % 10
units := hybrid % 10
//log.Printf("Hundreds:%d, Tens:%d, Units:%d\n", hundreds, tens, units)
if hundreds > 0 {
words = append(words, indianUnits[hundreds], "hundred")
}

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

switch tens {
case 0:
words = append(words, indianUnits[units])
case 1:
words = append(words, indianTeens[units])
break
default:
if units > 0 {
word := fmt.Sprintf("%s-%s", indianTens[tens], indianUnits[units])
words = append(words, word)
} else {
words = append(words, indianTens[tens])
}
break
}

hybridEnd:
// mega
if mega := indianMegas[idx]; mega != "" {
words = append(words, mega)
}
}

//log.Printf("Words length: %d\n", len(words))
return strings.Join(words, " ")
}
19 changes: 19 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,22 @@ func integerToTriplets(number int) []int {

return triplets
}

func integerToDHybrid(number int) []int {
hybrid := []int{}

startHybrid := false
for number > 0 {
if !startHybrid{
hybrid = append(hybrid, number%1000)
number = number / 1000
startHybrid = true
} else {
hybrid = append(hybrid, number%100)
number = number / 100
}

}

return hybrid
}

0 comments on commit bb890e9

Please sign in to comment.