From bb890e90069032712991cc374a912e762caae0ff Mon Sep 17 00:00:00 2001 From: Chilarai Mushahary Date: Thu, 13 Oct 2022 00:52:02 +0530 Subject: [PATCH] Added Indian conversion (and subcontinent) for currencies (#155) * Indian currency conversion (also for the subcontinent) * updated readme * Unwanted log commented out * Fixes to comments and added info about indian language --- README.md | 10 ++++-- en-in.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.go | 19 +++++++++++ 3 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 en-in.go diff --git a/README.md b/README.md index 924ef46..753be7f 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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 @@ -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 @@ -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) ๐Ÿ‡ธ๐Ÿ‡ช diff --git a/en-in.go b/en-in.go new file mode 100644 index 0000000..7cb4f74 --- /dev/null +++ b/en-in.go @@ -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, " ") +} diff --git a/utils.go b/utils.go index 085afcf..f15fa4c 100644 --- a/utils.go +++ b/utils.go @@ -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 +}