Skip to main content
tags updated
Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19
added 35 characters in body
Source Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19

I have tried to come-up with new different and simple method usingI am calling it a Single Loop String Triplets (SLST) and thus avoid the use of excessive arithmetic number operations, switches, array manipulations, reversing or splitting strings/arrays, or function recursion.

I have tried to come-up with simple method using a Single Loop String Triplets and thus avoid the use of excessive arithmetic number operations, switches, array manipulations, reversing or splitting strings/arrays, or function recursion.

I have tried to come-up with new different and simple method I am calling it a Single Loop String Triplets (SLST) and thus avoid the use of excessive arithmetic number operations, switches, array manipulations, reversing or splitting strings/arrays, or function recursion.

Tweeted twitter.com/StackCodeReview/status/1150057462894145538
code comment corrected: 3 digits changed to word: triplet
Source Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19
function NumToWordsInt(NumIn) {
//-------------------------------------------------------
//Convert Integer Number to English Words
//Using a Single Loop String Triplets (SLST) Methods
//Mohsen Alyafei 10 July 2019
//Call it for a whole number and fractional separately
//-------------------------------------------------------

 if (NumIn==0) return "Zero";
 var  Ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];
 var  Tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
 var Scale = ["", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion"];
 var N1, N2, Sep, L, j, i, h,Trplt,tns="", NumAll = "";
 NumIn += "";                                            //NumIn=NumIn.toString()
//----------------- code start -------------------
 NumIn = "0".repeat(NumIn.length * 2 % 3) + NumIn;       //Create shortest string triplets 0 padded
 j = 0;                                                  //Start with the highest triplet from LH
    for (i = NumIn.length / 3 - 1; i >= 0; i--) {        //Loop thru number of triplets from LH most
      Trplt = NumIn.substring(j, j + 3);                 //Get a triplet number starting from LH
      if (Trplt != "000") {                              //Skip empty triplets
        h = ""; //Init hundreds                          //-------inner code for 3 digit1 startstriplet
        Trplt[2] != "0" ? Sep="-":Sep=" ";               //Only if hyphen needed for nums 21 to 99
        N1 = Number(Trplt[0]);                           //Get Hundreds digit
        N2 = Number(Trplt.substr(1));                    //Get 2 lowest digits (00 to 99) 
        N2 > 19 ? tns = Tens[Number(Trplt[1])] + Sep + Ones[Number(Trplt[2])]:tns = Ones[N2]
        if (N1 > 0) h = Ones[N1] + " Hundred"            //Add " hundred" if needed
        Trplt = (h + " " + tns).trim() + " " + Scale[i]; //Create number with scale ----inner code ends
        NumAll = NumAll + Trplt + " ";                   //join the triplets scales to previous
      }
      j += 3;                                            //Go for next lower triplets (move to RH)
    }
//----------------- code end --------------------- 
 return NumAll.trim();                                   //Return trimming excess spaces
}
//
//
//================= for testing ================

document.getElementById('number').onkeyup = function () {
    document.getElementById('words').innerHTML = NumToWordsInt(document.getElementById('number').value);
};
<span id="words"></span>
<input id="number" type="text" />
function NumToWordsInt(NumIn) {
//-------------------------------------------------------
//Convert Integer Number to English Words
//Using a Single Loop String Triplets (SLST) Methods
//Mohsen Alyafei 10 July 2019
//Call it for a whole number and fractional separately
//-------------------------------------------------------

 if (NumIn==0) return "Zero";
 var  Ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];
 var  Tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
 var Scale = ["", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion"];
 var N1, N2, Sep, L, j, i, h,Trplt,tns="", NumAll = "";
 NumIn += "";                                            //NumIn=NumIn.toString()
//----------------- code start -------------------
 NumIn = "0".repeat(NumIn.length * 2 % 3) + NumIn;       //Create shortest string triplets 0 padded
 j = 0;                                                  //Start with the highest triplet from LH
    for (i = NumIn.length / 3 - 1; i >= 0; i--) {        //Loop thru number of triplets from LH most
      Trplt = NumIn.substring(j, j + 3);                 //Get a triplet number starting from LH
      if (Trplt != "000") {                              //Skip empty triplets
        h = ""; //Init hundreds                          //-------inner code for 3 digit starts
        Trplt[2] != "0" ? Sep="-":Sep=" ";               //Only if hyphen needed for nums 21 to 99
        N1 = Number(Trplt[0]);                           //Get Hundreds digit
        N2 = Number(Trplt.substr(1));                    //Get 2 lowest digits (00 to 99) 
        N2 > 19 ? tns = Tens[Number(Trplt[1])] + Sep + Ones[Number(Trplt[2])]:tns = Ones[N2]
        if (N1 > 0) h = Ones[N1] + " Hundred"            //Add " hundred" if needed
        Trplt = (h + " " + tns).trim() + " " + Scale[i]; //Create number with scale ----inner code ends
        NumAll = NumAll + Trplt + " ";                   //join the triplets scales to previous
      }
      j += 3;                                            //Go for next lower triplets (move to RH)
    }
//----------------- code end --------------------- 
 return NumAll.trim();                                   //Return trimming excess spaces
}
//
//
//================= for testing ================

document.getElementById('number').onkeyup = function () {
    document.getElementById('words').innerHTML = NumToWordsInt(document.getElementById('number').value);
};
<span id="words"></span>
<input id="number" type="text" />
function NumToWordsInt(NumIn) {
//-------------------------------------------------------
//Convert Integer Number to English Words
//Using a Single Loop String Triplets (SLST) Methods
//Mohsen Alyafei 10 July 2019
//Call it for a whole number and fractional separately
//-------------------------------------------------------

 if (NumIn==0) return "Zero";
 var  Ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];
 var  Tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
 var Scale = ["", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion"];
 var N1, N2, Sep, L, j, i, h,Trplt,tns="", NumAll = "";
 NumIn += "";                                            //NumIn=NumIn.toString()
//----------------- code start -------------------
 NumIn = "0".repeat(NumIn.length * 2 % 3) + NumIn;       //Create shortest string triplets 0 padded
 j = 0;                                                  //Start with the highest triplet from LH
    for (i = NumIn.length / 3 - 1; i >= 0; i--) {        //Loop thru number of triplets from LH most
      Trplt = NumIn.substring(j, j + 3);                 //Get a triplet number starting from LH
      if (Trplt != "000") {                              //Skip empty triplets
        h = ""; //Init hundreds                          //-------inner code for 1 triplet
        Trplt[2] != "0" ? Sep="-":Sep=" ";               //Only if hyphen needed for nums 21 to 99
        N1 = Number(Trplt[0]);                           //Get Hundreds digit
        N2 = Number(Trplt.substr(1));                    //Get 2 lowest digits (00 to 99) 
        N2 > 19 ? tns = Tens[Number(Trplt[1])] + Sep + Ones[Number(Trplt[2])]:tns = Ones[N2]
        if (N1 > 0) h = Ones[N1] + " Hundred"            //Add " hundred" if needed
        Trplt = (h + " " + tns).trim() + " " + Scale[i]; //Create number with scale ----inner code ends
        NumAll = NumAll + Trplt + " ";                   //join the triplets scales to previous
      }
      j += 3;                                            //Go for next lower triplets (move to RH)
    }
//----------------- code end --------------------- 
 return NumAll.trim();                                   //Return trimming excess spaces
}
//
//
//================= for testing ================

document.getElementById('number').onkeyup = function () {
    document.getElementById('words').innerHTML = NumToWordsInt(document.getElementById('number').value);
};
<span id="words"></span>
<input id="number" type="text" />
highlights bold text
Source Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19
Loading
edited body
Source Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19
Loading
added additional information hopefully usefull
Source Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19
Loading
typo
Source Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19
Loading
added 54 characters in body
Source Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19
Loading
added additional information useful
Source Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19
Loading
edited body
Source Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19
Loading
Source Link
Mohsen Alyafei
  • 599
  • 3
  • 5
  • 19
Loading