LoginRegister
Nintendo Wii / Wii U Forum
+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Raging Clue Budo's Avatar
    Member #
    34113
    Join Date
    Sep 2007
    Location
    Canadia
    Posts
    727
    Friends
    0
    Wii Friend Code: $post[field5]

    Anyone know a bit of Javascript?

    I have a form where the user inputs a number, selects a size, and then the total is displayed in a third box. The problem is, I want the total to be formatted to two decimal places. The answer has to be simple, but I can't find anything:

    <script language = JavaScript>

    function calculate()

    {

    A = document.form1.txtFirstNumber.value
    B = document.form1.txtSecondNumber.value

    A = Number(A)
    B = Number(B)
    C = (A * B )
    document.form1.txtThirdNumber.value = C

    }

    </script>

  2. #2
    Gearhead Surf Coder [DT]'s Avatar
    Member #
    33107
    Join Date
    Aug 2007
    Location
    Vilano Beach, FL
    Posts
    1,329
    Friends
    0
    Wii Friend Code: $post[field5]
    Is the Javascript parser later than 1.5? Then you can use a nice built-in function: toFixed()

    Like so:

    var v = 25.567;
    return v.toFixed(2);

    (which will return 25.56)

    Again, this is specifically for later JS implementations.

    Oh BTW, if you need a function for earlier JS, I'm sure I have something - I used to use the Math.Round() function (which rounded up to the nearest int) with a little creative math to pad the decimal places (simple * and / by a factor of 10).

    Also you might want to check for a numeric value (isNan(), or parse out the numeric portion parseInt(), or parseFloat()).
    Last edited by [DT]; 01-29-2008 at 03:59 PM.
    "I want to be more like the ocean, no talking, all action"
    - Jane's Addiction

    "Ga-blaaaaagh!!"
    - Liliane Eva

    "The grammar corrections around here are officially old and tired."
    - Celeste

  3. #3
    Raging Clue Budo's Avatar
    Member #
    34113
    Join Date
    Sep 2007
    Location
    Canadia
    Posts
    727
    Friends
    0
    Wii Friend Code: $post[field5]
    It's after 1.5.

    Thanks, I'll give that a try

  4. #4
    Gearhead Surf Coder [DT]'s Avatar
    Member #
    33107
    Join Date
    Aug 2007
    Location
    Vilano Beach, FL
    Posts
    1,329
    Friends
    0
    Wii Friend Code: $post[field5]
    Quote Originally Posted by Budo
    It's after 1.5.

    Thanks, I'll give that a try

    No problem.

    You might also want to use the later DOM for finding elements vs. having to traverse a tree:

    document.getElementById()

    Then you can short form the element/object like so:

    var f_name = document.getElementById('TextBoxFirstName');

    var f_val = f_name.value;

    f_val.style.display = 'block';

    Handy if you need to get/set properties for the element in a few places.
    "I want to be more like the ocean, no talking, all action"
    - Jane's Addiction

    "Ga-blaaaaagh!!"
    - Liliane Eva

    "The grammar corrections around here are officially old and tired."
    - Celeste

Ads

Posting Permissions

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