Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Digits to words - (5 to Five)

    Code:
    <script language="JavaScript1.2" type="text/javascript">
    
    function num2words (num) {
      num = num.replace(/[^0-9]/, ""); //removes all non-numeric characters.
      break3 = new Array ("","Thousand","Million","Billion","Trillion","Quadrillion","Quintillion");
      one2twenty = new Array ("","One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
      tens = new Array("","","Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");
      num_split = new Array();
      for ( x=Math.ceil(num.length/3); x>0; x-- ) {
        if ( num.length > 3 ) {
          num_split[num_split.length] = num.substring(num.length-3,num.length);
          num = num.substring(0, num.length-3);
        } else {
          num_split[num_split.length] = num;
          num = "";
        }
      }
      num_split = num_split.reverse();
      ret = "";
      for ( x in num_split ) {
        suff = num_split.length-x-1;
        val = num_split[x];
        if ( val < 20) {
          val = one2twenty[val];
          hun = "";
        } else {
          if ( val >= 100 ) {
            hun = one2twenty[val.charAt(0)]+" Hundred ";
            ten = val.charAt(1);
            one = val.charAt(2);
          } else {
            hun = "";
            ten = val.charAt(0);
            one = val.charAt(1);
          }
          if ( one == 0 ) {
            val = tens[ten]+one2twenty[one];
          } else if ( ten >= 2 ) {
            val = tens[ten]+"-"+one2twenty[one];
          } else {
            val = one2twenty[ten.one];
          }
        }
        ret += hun+val+" "+break3[suff];
        if ( suff != "" ) ret += ", ";
      }
      return ret;
    }
    
    document.write(num2words("1275h33"));
    
    </script>

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You posted this in the PHP forum as a PHP script

    Getting a string from a number format (eg changing 2 into two)

    Just if PHP coders wanted to do this...

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ya, just finally got around to doing it in JS. Seemed useful enough...

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, I would have said so. It's a very nice function. Plus, not all people have PHP.


  • #5
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,453
    Thanks
    1
    Thanked 21 Times in 19 Posts
    What about decimal points, or negative numbers?
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if ( num.charAt(0) == "-" ) {
    //negative
    }

    too lazy to do it now, maybe later.

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could be a method for strings and numbers! Only changes are
    Code:
    Number.prototype.toEnglish = function() 
    {
    	return this.toString().toEnglish();
    }
    
    String.prototype.toEnglish = function() {
      num = this.replace(/[^0-9]/, ""); //removes all non-numeric characters.
    And, of course, the usage
    Code:
    document.write(("1275h33").toEnglish());
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •