Results 1 to 5 of 5

Thread: Any one see this in Javascript

  1. #1
    Join Date
    Apr 2007
    Location
    Southwest France
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Any one see this in Javascript

    I wonder if any one can help me, I've been searching the net for a javascript script, which when you type in any figure it will return that figure in written words.

    For example: 200.10 returns two hundred dollars and ten cents,

    and : 1.1 = one dollar and one cent (note single and plural dollar/dollars and cent/cents)

    I've found scripts in PHP & VB but as I know less about these then javascript, can I convert these to javascript (easy) ?

    I don't know where to start, I'm not that great with javascript but could modify and change a working script to fit my bill.

    Many Thanks for any help.

    Steve Sea

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    I've found scripts in PHP & VB but as I know less about these then javascript, can I convert these to javascript (easy) ?
    yes you can convert the script to javascript. If you do not know how, post the code wrapping it within [code] tags and someone here will translate for you.

    You can post either script, however more people here know PHP than VB, so chances are you would have a faster turn around if you posted in PHP, although since there isnt a VB section maybe there are VB addicts here and just weren't speaking up.

  3. #3
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    open notepad and copy this in and save as towords.js:

    Code:
    // Convert numbers to words
    // copyright 25th July 2006, by Stephen Chapman http://javascript.about.com
    // permission to use this Javascript on your web page is granted
    // provided that all of the code (including this copyright notice) is
    // used exactly as shown (you can change the numbering system if you wish)
    // American Numbering System
    var th = ['','thousand','million', 'billion','trillion'];
    // uncomment this line for English Number System
    // var th = ['','thousand','million', 'milliard','billion'];
    
    var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen']; var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; function toWords(s){s = s.replace(/[\, ]/g,''); if (s != String(parseFloat(s))) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if ((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str += dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str += 'dollars and '; for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';} return str.replace(/\s+/g,' ');}
    add this to the <head> of the page:

    Code:
    <script type="text/javascript" src="towords.js">
    </script>
    and where you want your conversions, use this:

    Code:
    <form name="test"><input type="text" name="inum" value="" size="18"><br><br><input type="button" value="To Words" onclick="test.rnum.value = toWords(test.inum.value);"><br><br><textarea name="rnum" cols="40" rows="5"></textarea></form>
    This was found here

    I edited it slightly to add the "dollars and cents" part.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  4. #4
    Join Date
    Apr 2007
    Location
    Southwest France
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Many Thanks Boogyman & Blizzard

    1stly Blizzard can you just double check the "towords.js" script you posted as I think you may have posted the original and dollar & cents are not coming up but I do like the script nice and short.
    And I must say thanks for adding the other code as it helps me to have a working model to play with and then I can pull it to bits and see how it works, thats how I leaned to fix bikes and cars, the good thing with codes you don't end up with a garage full of bits.


    2ndly Boogyman this is the best script I've come across bit long winded but will do the job nicely.

    Much appreciated

    Steve

    Code:
    <?php
    
    //*************************************************************
    // this function converts an amount into alpha words
    // and adds the words dollars and cents.  Pass it a float.
    // works up to 999,999,999.99 dollars - send me a check
    //*************************************************************
    
    function makewords($numval)
    {
    $moneystr = "";
    // handle the millions
    $milval = (integer)($numval / 1000000);
    if($milval > 0)
      {
      $moneystr = getwords($milval) . " Million";
      }
    
    // handle the thousands
    $workval = $numval - ($milval * 1000000); // get rid of millions
    $thouval = (integer)($workval / 1000);
    if($thouval > 0)
      {
      $workword = getwords($thouval);
      if ($moneystr == "")
        {
        $moneystr = $workword . " Thousand";
        }
      else
        {
        $moneystr .= " " . $workword . " Thousand";
        }
      }
    
    // handle all the rest of the dollars
    $workval = $workval - ($thouval * 1000); // get rid of thousands
    $tensval = (integer)($workval);
    if ($moneystr == "")
      {
      if ($tensval > 0)
        {
        $moneystr = getwords($tensval);
        }
      else
        {
        $moneystr = "Zero";
        }
      }
    else // non zero values in hundreds and up
      {
      $workword = getwords($tensval);
      $moneystr .= " " . $workword;
      }
    
    // plural or singular 'dollar'
    $workval = (integer)($numval);
    if ($workval == 1)
      {
      $moneystr .= " Dollar And ";
      }
    else
      {
      $moneystr .= " Dollars And ";
      }
    
    // do the pennies - use printf so that we get the
    // same rounding as printf
    $workstr = sprintf("%3.2f",$numval); // convert to a string
    $intstr = substr($workstr,strlen - 2, 2);
    $workint = (integer)($intstr);
    if ($workint == 0)
      {
      $moneystr .= "Zero";
      }
    else
      {
      $moneystr .= getwords($workint);
      }
    if ($workint == 1)
      {
      $moneystr .= " Cent";
      }
    else
      {
      $moneystr .= " Cents";
      }
    
    // done - let's get out of here!
    return $moneystr;
    }
    
    //*************************************************************
    // this function creates word phrases in the range of 1 to 999.
    // pass it an integer value
    //*************************************************************
    function getwords($workval)
    {
    $numwords = array(
      1 => "One",
      2 => "Two",
      3 => "Three",
      4 => "Four",
      5 => "Five",
      6 => "Six",
      7 => "Seven",
      8 => "Eight",
      9 => "Nine",
      10 => "Ten",
      11 => "Eleven",
      12 => "Twelve",
      13 => "Thirteen",
      14 => "Fourteen",
      15 => "Fifteen",
      16 => "Sixteen",
      17 => "Seventeen",
      18 => "Eightteen",
      19 => "Nineteen",
      20 => "Twenty",
      30 => "Thirty",
      40 => "Forty",
      50 => "Fifty",
      60 => "Sixty",
      70 => "Seventy",
      80 => "Eighty",
      90 => "Ninety");
    
    // handle the 100's
    $retstr = "";
    $hundval = (integer)($workval / 100);
    if ($hundval > 0)
      {
      $retstr = $numwords[$hundval] . " Hundred";
      }
    
    // handle units and teens
    $workstr = "";
    $tensval = $workval - ($hundval * 100); // dump the 100's
    if (($tensval < 20) && ($tensval > 0))// do the teens
      {
      $workstr = $numwords[$tensval];
      }
    else // got to break out the units and tens
      {
      $tempval = ((integer)($tensval / 10)) * 10; // dump the units
      $workstr = $numwords[$tempval]; // get the tens
      $unitval = $tensval - $tempval; // get the unit value
      if ($unitval > 0)
        {
        $workstr .= " " . $numwords[$unitval];
        }
      }
    
    // join all the parts together and leave
    if ($workstr != "")
      {
      if ($retstr != "")
        {
        $retstr .= " " . $workstr;
        }
      else
        {
        $retstr = $workstr;
        }
      }
    return $retstr;
    }
    
    
    $floatval = 1.02;
    $myresult = makewords($floatval);
    echo "$myresult<br>";
    printf("The value is %0.2f<br>",$floatval);
    
    
    ?>

  5. #5
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    I will double check ASAP.. I guess I should test my own posts before getting away from it, eh?

    I used to be REALLY bad at that..

    (not my fault, I learned it from Twey )
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

Bookmarks

Posting Permissions

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