Results 1 to 3 of 3

Thread: Adding Values in input tag

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

    Default Adding Values in input tag

    Hi Chaps
    Got a problem, I'm using a small script when you select a button with a number on it, it returns that number in written word,

    ie. press button 10 and the word "Ten" pops up this is all working OK and the back bone of the code is below what I'm trying to re produce now is if someone presses "100 + 30" it will return (in figures) "130" so is there anyway of adding up the values in the Input tag and displaying it. if you need to see why you can pop to my website, its to place the input in the amount box so visitors can double check the amounts they have entered

    www.euro-cheque.com

    <input name="Thousands" type="button" onclick="showNumber1('One thousand');" value="1000"><br>

    Any help or comments are appreciated

    Steve C.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, your script is obfuscated, so I'm not going to try to give you any details as to how to edit it. However, I can say that when adding values (strings really) like:

    Code:
    alert("100"+"30");
    you will get:

    10030

    If this is the problem, you need to make them into numbers first, example:

    Code:
    var n1="100";
    var n2="30";
    alert(parseInt(n1)+parseInt(n2));
    Now you get:

    130

    There are less processor intensive ways to turn a string of this type (one that contains only numeric characters) into a number in javascript, you could do:

    Code:
    var n1="100";
    var n2="30";
    alert((n1-0)+(n2-0));
    and still get:

    130

    I mention this only if it is workable in your code. If it is, it is better, if not, use the parseInt() method.

    Also, if you are dealing with decimal points, you will need to use the parseFloat() method, or figure out a way to first get the decimal point out, do the math, and then put the decimal point back in the appropriate spot.

    It is always best to avoid parseFloat if at all possible. It's results can sometimes be off by a minuscule amount. When that happens, you get a repeating decimal value like:

    .299999999999999

    instead of:

    .30

    Using parseFloat() is also the most resource intensive method of the three I've mentioned.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    I understand my code is hard to follow I have encrypted the javascript to TRY and stop my code getting ripped, so sorry for that.
    I think I will play with the javascript example, and see if I get any results and if it make the script that much heaver.

    Many Thank

    Steve C.

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
  •