Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Converting "text" number to an actual number

    In recent postings I was shown how to pass numbers from one web page to another. That works great. The problem I have now is the numbers that were passed are text instead of numbers. Listed below are the numbers that were passed:

    num1 = 100
    num2 = 200
    num3 = 300

    If I were to add these numbers together on Page2.htm, the result would look like 100200300 instead of 600. Any ideas as I would convert these to actual numbers?




    The posting where I obtain the coding:

    click here

    http://www.codingforums.com/showthre...241#post162241

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    parseInt( string, 10 ); // Integers with a radix of 10. (IOW, decimal integers)

    parseFloat( string ); // Floating point numbers, radix of 10. (IOW, decimal floating point numbers)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also the Number function is another choice.

  • #4
    New Coder
    Join Date
    Dec 2003
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    It still is not converting the text number to a number

    Thanks guys for your help. I'm still having problems, however, converting the textstring numbers to numeric numbers. To test the conversion, I created two numbers, one as a text string and the other as a numeric number. I used the number function, but it didn't convert it. It should have returned the answer:

    350.

    Instead, it returned:

    250100.

    Any ideas? I also used the parseInt and got the same results. Listed below are the codes I used.





    <script type="text/javascript" language="javascript">
    var num1=250
    var num2="100"
    Number(num2)
    newnum=num1+num2
    alert (newnum)
    </script>






    <script type="text/javascript" language="javascript">
    var num1=250
    var num2="100"
    Number(num2)
    parseInt(num2,10);
    newnum=num1+num2
    alert(newnum)
    </script>

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Ah, the input is not changed when you use those functions - the output from them is changed, however. An example:
    Code:
    var
        sNumber = '100.1', // => [string '100.1']
        iNumber = parseInt( sNumber ), // => [number 100]
        nNumber = parseFloat( sNumber ); // =>[number 100.1]
    
    /* The results?
        sNumber is still a string '100.1'
        iNumber is a number 100
        and nNumber is a number 100.1
    */
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    New Coder
    Join Date
    Dec 2003
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks

    thanks, that did it. I appreciate the help. I'm glad I found this place.


  •  

    Posting Permissions

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