0

I'm trying to display numbers in words that are in Indian numbering format on keypress up to 16 digit number but the jquery function works up to only 10 digits. here is my code snippet

<div class="form-group mb-3 customtooltip col-6">
        <p>
           <input type="text" id="text" class="mb-3 form-control allow_decimal" />            
            <b><span class="spnWord"></span></b>
        </p>
</div> 

JS function for conversion I got through google search

$('.allow_decimal').keyup(function (event) {

      $(this).val(function (index, value) {
            return value.replace(/\D/g, '').replace(/(\d)(?=(\d\d)+\d$)/g, "$1,"); // to add commas 
       });

       $(this).closest('.form-group').find('.spnWord').text(price_in_words(this.value));
});

function price_in_words(price1) {
            console.log(price1);
            // Just remove commas and periods - regex can do any chars
            price = price1.replace(/([,€])+/g, '');
            var sglDigit = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"],
                dblDigit = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"],
                tensPlace = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"],
                handle_tens = function (dgt, prevDgt) {
                    return 0 == dgt ? "" : " " + (1 == dgt ? dblDigit[prevDgt] : tensPlace[dgt])
                },
                handle_utlc = function (dgt, nxtDgt, denom) {
                    return (0 != dgt && 1 != nxtDgt ? " " + sglDigit[dgt] : "") + (0 != nxtDgt || dgt > 0 ? " " + denom : "")
                };
            var str = "",
                digitIdx = 0,
                digit = 0,
                nxtDigit = 0,
                words = [];
            if (price += "", isNaN(parseInt(price))) str = "";
            else if (parseInt(price) > 0 && price.length <= 10) {
                for (digitIdx = price.length - 1; digitIdx >= 0; digitIdx--) switch (digit = price[digitIdx] - 0, nxtDigit = digitIdx > 0 ? price[digitIdx - 1] - 0 : 0, price.length - digitIdx - 1) {
                    case 0:
                        words.push(handle_utlc(digit, nxtDigit, ""));
                        break;
                    case 1:
                        words.push(handle_tens(digit, price[digitIdx + 1]));
                        break;
                    case 2:
                        words.push(0 != digit ? " " + sglDigit[digit] + " Hundred" + (0 != price[digitIdx + 1] && 0 != price[digitIdx + 2] ? " and" : "") : "");
                        break;
                    case 3:
                        words.push(handle_utlc(digit, nxtDigit, "Thousand"));
                        break;
                    case 4:
                        words.push(handle_tens(digit, price[digitIdx + 1]));
                        break;
                    case 5:
                        words.push(handle_utlc(digit, nxtDigit, "Lakh"));
                        break;
                    case 6:
                        words.push(handle_tens(digit, price[digitIdx + 1]));
                        break;
                    case 7:
                        words.push(handle_utlc(digit, nxtDigit, "Crore"));
                        break;
                    case 8:
                        words.push(handle_tens(digit, price[digitIdx + 1]));
                        break;
                    case 9:
                        words.push(0 != digit ? " " + sglDigit[digit] + " Hundred" + (0 != price[digitIdx + 1] || 0 != price[digitIdx + 2] ? " and" : " Crore") : "")
                }
                str = words.reverse().join("")
            }    
            console.log(str);
            return str

        }

Current Jquery work up to 10 digit numbers like

if the number is 1,20,10,00,000 the word conversion will be ' One Hundred and Twenty Crore Ten Lakh'.

the number greater than 10 digit return empty

if the number is 10,20,10,00,000 the word conversion should be One thousand twenty crores ten lakh

James Z
  • 12,209
  • 10
  • 24
  • 44
Dark S
  • 308
  • 2
  • 15

1 Answers1

0

I split numbers to achieve desire result, although its cant be called as accurate one it solved my issue.

$('.allow_decimal').keyup(function (event) {
            $(this).val(function (index, value) {

                return value
                    .replace(/\D/g, '')
                    .replace(/(\d)(?=(\d\d)+\d$)/g, "$1,")
                    ;
            });
            var money = this.value;            
            var length = money.length;
            if (length < 15) {
                $(this).closest('.form-group').find('.spnWord').text(price_in_words(this.value));
            } else {
                var moneys = money.replace(new RegExp(',', 'g'), '');                             
                var lacvalue = moneys.substring(moneys.length - 7, moneys.length);
                var croreValue = moneys.substr(0, moneys.length - 7);
                $(this).closest('.form-group').find('.spnWord').text(price_in_words(croreValue) + ' Crore ' + price_in_words(lacvalue));                
            }
        });
Dark S
  • 308
  • 2
  • 15
  • 1
    Please check this out: https://stackoverflow.com/questions/62795243/indian-numbering-system-how-to-convert-into-the-official-and-common-use-numberi. Hope it provides the answers. – Mohsen Alyafei Jul 08 '20 at 13:16