|
| 1 | +package ntw |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +func init() { |
| 9 | + // register the language |
| 10 | + Languages["en-in"] = Language{ |
| 11 | + Name: "Indian English", |
| 12 | + Aliases: []string{"en", "en-in", "indian", "english"}, |
| 13 | + Flag: "🇮🇳", |
| 14 | + |
| 15 | + IntegerToWords: IntegerToEnIn, |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// IntegerToEnIn converts an integer to Indian English words |
| 20 | +func IntegerToEnIn(input int) string { |
| 21 | + var indianMegas = []string{"", "thousand", "lakh", "crore", "arab", "kharab", "neel", "padma", "shankh", "mahashankh"} |
| 22 | + var indianUnits = []string{"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"} |
| 23 | + var indianTens = []string{"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"} |
| 24 | + var indianTeens = []string{"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"} |
| 25 | + |
| 26 | + //log.Printf("Input: %d\n", input) |
| 27 | + words := []string{} |
| 28 | + |
| 29 | + if input < 0 { |
| 30 | + words = append(words, "minus") |
| 31 | + input *= -1 |
| 32 | + } |
| 33 | + |
| 34 | + // split integer in hybrids |
| 35 | + var hybrids []int |
| 36 | + hybrids = integerToDHybrid(input) |
| 37 | + |
| 38 | + |
| 39 | + // log.Printf("Hybrids: %v\n", hybrids) |
| 40 | + |
| 41 | + // zero is a special case |
| 42 | + if len(hybrids) == 0 { |
| 43 | + return "zero" |
| 44 | + } |
| 45 | + |
| 46 | + // iterate over hybrids |
| 47 | + for idx := len(hybrids) - 1; idx >= 0; idx-- { |
| 48 | + hybrid := hybrids[idx] |
| 49 | + //log.Printf("hybrid: %d (idx=%d)\n", hybrid, idx) |
| 50 | + |
| 51 | + // nothing todo for empty hybrid |
| 52 | + if hybrid == 0 { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + // three-digits |
| 57 | + hundreds := hybrid / 100 % 10 |
| 58 | + tens := hybrid / 10 % 10 |
| 59 | + units := hybrid % 10 |
| 60 | + //log.Printf("Hundreds:%d, Tens:%d, Units:%d\n", hundreds, tens, units) |
| 61 | + if hundreds > 0 { |
| 62 | + words = append(words, indianUnits[hundreds], "hundred") |
| 63 | + } |
| 64 | + |
| 65 | + if tens == 0 && units == 0 { |
| 66 | + goto hybridEnd |
| 67 | + } |
| 68 | + |
| 69 | + switch tens { |
| 70 | + case 0: |
| 71 | + words = append(words, indianUnits[units]) |
| 72 | + case 1: |
| 73 | + words = append(words, indianTeens[units]) |
| 74 | + break |
| 75 | + default: |
| 76 | + if units > 0 { |
| 77 | + word := fmt.Sprintf("%s-%s", indianTens[tens], indianUnits[units]) |
| 78 | + words = append(words, word) |
| 79 | + } else { |
| 80 | + words = append(words, indianTens[tens]) |
| 81 | + } |
| 82 | + break |
| 83 | + } |
| 84 | + |
| 85 | + hybridEnd: |
| 86 | + // mega |
| 87 | + if mega := indianMegas[idx]; mega != "" { |
| 88 | + words = append(words, mega) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + //log.Printf("Words length: %d\n", len(words)) |
| 93 | + return strings.Join(words, " ") |
| 94 | +} |
0 commit comments