Results 1 to 6 of 6
  1. #1
    Lounger
    Join Date
    Jun 2001
    Location
    South Australia, Australia
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    addition operator in javascript

    Hi,

    I think I'm going crazy...I have a simple script to do some calculations. If I use the *, -, or / operators, the script works fine. When I use the + operator to add, it doesn't. Instead of the output from 2+3 being 5, it's 23. The numbers being used are user-entered from a form, but I wouldn't have thought that should make a difference, since the other 3 basic operators work just fine.

    Anybody seen this before? Anybody help me retain my sanity?

    Thanks

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: addition operator in javascript

    The "+" is also used as a concatenate operator. So if JS thinks you have a string, it will concatenate rather than add.

    Here's what I get in the help on convertision to numbers:
    <hr>As JScript is a loosely-typed language, variables in JScript technically have no fixed type. Instead, they have a type equivalent to the type of the value they contain. It is possible, under some circumstances, to force the automatic conversion (or coercion) of a variable or a piece of data into a different type. Numbers can easily be included in strings, but strings cannot be included directly in numbers, so explicit conversion functions, parseInt() and parseFloat(), are provided. <hr>
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Lounger
    Join Date
    Jun 2001
    Location
    South Australia, Australia
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: addition operator in javascript

    Thanks Geoff!

    The parseInt fixed it. One other question...I'm using the Math.round function, which is working fine. Is there some function that will let you round to two decimal places instead of zero?

  4. #4
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: addition operator in javascript

    I'm not that much up with Javascript. My guess would be to muliply by 100, use Math.round, and divide it by 100.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  5. #5
    New Lounger
    Join Date
    May 2001
    Location
    New York City, NY
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: addition operator in javascript

    round takes two arguments. just use "2" as the second argument.

    Stan Scott

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    PA
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: addition operator in javascript-MULTI-Re: addition operator in javascript

    Geoff,

    You didn't post the relevent portion of your script, but it is clear that JS thinks the input is not numeric and is cocatenating it. The easy way around this is to first assign the input to varibles

    ipt1 = the first entered number
    ipt2 = the second number

    Then assign the mathmatical result to a variable

    sum12.value = ipt1.value + ipt2.value

    Then write it to the document

    document.write("The sum of the two numbers is " + sum12.value)

    You may also want to do some error trapping

    if(isNaN(ipt1) {
    document.write("your entry for Input One, " + ipt1 + " is not a number. You must enter a number")
    }

    The method isNaN returns true if the parameter is not a number.

    Good luck

Posting Permissions

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