Numbers from a text box for calculation

Discussion in 'Javascript' started by adam.waterfield@gmail.com, Mar 7, 2006.

  1. Guest

    Hi guys,
    I know this is a really basic problem, but I am buggered if I can
    remember or find the soultion to it.

    I have the two text boxes on a form, both of which a user enters a
    number. I simple need to ADD THESE UP!

    I know, its daft, but no matter what I do, it just adds them together.

    Example: 1+1 is clearly 2, but it outputs 11!

    An example of my code:

    var loan = document.loandata.loan.value;
    var other = document.loandata.other.value;

    total = (loan+other);

    Any help would be very much welcomed!

    Adam
     
    , Mar 7, 2006
    #1
    1. Advertising

  2. Amie Guest

    Hi,

    You need to convert string to number. Use parseFloat(loan) or
    parseInt(loan).

    Hope it helps,
    Amie
     
    Amie, Mar 7, 2006
    #2
    1. Advertising

  3. writes:

    Hi,

    using the SpiderMonkey interpreter:

    aundro@paddy:~$ js
    js> (+ "1") + (+ "1")
    2
    js>
    aundro@paddy:~$

    Simply: the '+' unary operator, applied to a string, transforms it
    into a number, if possible. (NaN otherwise)

    Arnaud




    > Hi guys,
    > I know this is a really basic problem, but I am buggered if I can
    > remember or find the soultion to it.
    >
    > I have the two text boxes on a form, both of which a user enters a
    > number. I simple need to ADD THESE UP!
    >
    > I know, its daft, but no matter what I do, it just adds them together.
    >
    > Example: 1+1 is clearly 2, but it outputs 11!
    >
    > An example of my code:
    >
    > var loan = document.loandata.loan.value;
    > var other = document.loandata.other.value;
    >
    > total = (loan+other);
    >
    > Any help would be very much welcomed!
    >
    > Adam
    >


    --
    Arnaud DIEDEREN
    Software Developer
    IONIC Software
    Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
    Tel: +32.4.3640364 - Fax: +32.4.2534737
    mailto:
    http://www.ionicsoft.com
     
    Arnaud Diederen, Mar 7, 2006
    #3
  4. Amie wrote:

    > You need to convert string to number. Use parseFloat(loan) or
    > parseInt(loan).


    The `+' operator usually suffices. When integer values are required,
    the base (e.g. 10) should be passed as second argument of parseInt().

    Please read the FAQ <URL:http://jibbering.com/faq/> before you try to
    provide advice here.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Mar 7, 2006
    #4
  5. Amie wrote:

    > You need to convert string to number. Use parseFloat(loan) or
    > parseInt(loan).


    The unary `+' operator usually suffices. When integer values are required,
    the base (e.g. 10) should be passed as second argument of parseInt().

    Please read the FAQ <URL:http://jibbering.com/faq/> before you try to
    provide advice here.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Mar 7, 2006
    #5
  6. JRS: In article <>,
    dated Tue, 7 Mar 2006 07:34:29 remote, seen in
    news:comp.lang.javascript, posted :

    >I know this is a really basic problem, but I am buggered if I can
    >remember or find the soultion to it.


    Then you have evidently not searched competently, since the answer is
    given clearly enough in the newsgroup FAQ, which all should read before
    posting questions.


    >I have the two text boxes on a form, both of which a user enters a
    >number. I simple need to ADD THESE UP!
    >
    >I know, its daft, but no matter what I do, it just adds them together.


    That's what you want. Adding up and adding together have the same
    meaning in English. What you get, however, is concatenation.


    >Example: 1+1 is clearly 2, but it outputs 11!
    >
    >An example of my code:
    >
    >var loan = document.loandata.loan.value;
    >var other = document.loandata.other.value;
    >
    >total = (loan+other);


    Parentheses superfluous.

    The efficient way is to use the unary + operator. You could do
    total = +loan + +other ;
    but I'd use
    var loan = +document.loandata.loan.value
    var other = +document.loandata.other.value
    total = loan + other




    JRS: In article <>,
    dated Tue, 7 Mar 2006 07:47:41 remote, seen in
    news:comp.lang.javascript, Amie <> posted :

    >You need to convert string to number. Use parseFloat(loan) or
    >parseInt(loan).
    >
    >Hope it helps,


    One should also read the FAQ before answering, in order to avoid
    demonstrating inadequate understanding.

    Routine parseInt() should always be given two parameters, except when
    using only one is necessary or provably safe. And it is only necessary
    to use it if the numeric part of the input may be followed by non-
    whitespace. Unary + is both brief and efficient; though in coursework
    it may need explaining.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Mar 7, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mariame

    Only numbers in text Box

    Mariame, Aug 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    722
    Mark Rae
    Aug 19, 2004
  2. Replies:
    1
    Views:
    1,160
    David Wier
    Aug 5, 2005
  3. Gnarlodious
    Replies:
    4
    Views:
    17,645
    elizas
    May 5, 2010
  4. Neredbojias
    Replies:
    3
    Views:
    478
    Neredbojias
    Oct 4, 2005
  5. Petyr David

    Text Box drop down - consecutive numbers

    Petyr David, Aug 29, 2007, in forum: Javascript
    Replies:
    0
    Views:
    110
    Petyr David
    Aug 29, 2007
Loading...

Share This Page