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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exponent to number

    How to convert an exponential value to numeric value using java script
    Ex:
    1.700000000000001e+308 converted to be its numeric value.

    Can anyone please help me to achieve this using jquery/java script?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by varalakshmi View Post
    How to convert an exponential value to numeric value using java script
    Ex:
    1.700000000000001e+308 converted to be its numeric value.

    Can anyone please help me to achieve this using jquery/java script?

    Code:
    <script type = "text/javascript">
    
    var x  = (1.23e+20).toFixed(6);    // Returns "123000000000000000000.000000" - note added zeroes
    alert (x);
    var y = (1.23e-10).toFixed(2);    // Returns "0.00"
    alert (y);
    
    </script>
    1.700000000000001e+308 is too large a number for Javascript to handle.

    It is not as cold as it was yesterday, but that's probably because it's a bit warmer. - BBC Scotland News.
    Last edited by Philip M; 11-05-2012 at 08:23 PM. Reason: typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,630 Times in 4,593 Posts
    Quote Originally Posted by Philip M View Post
    1.700000000000001e+308 is too large a number for Javascript to handle.
    Ummm...actually, the largest number JS can handle is a tad larger than that.

    1.7976931348623157 e +308

    I *think* what Philip means is that JS can't convert a number that large to NON-scientific notation.

    If it were converted, it would be 309 digits long. No human could make sense of it, so the scientific notation is much better for this purpose.

    I only tested on Chrome, but the largest exponent JS will convert to fixed notation in Chrome is e+20. After that, it reverts to always using scientific notation.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...actually, the largest number JS can handle is a tad larger than that.

    1.7976931348623157 e +308

    I *think* what Philip means is that JS can't convert a number that large to NON-scientific notation.

    If it were converted, it would be 309 digits long. No human could make sense of it, so the scientific notation is much better for this purpose.

    I only tested on Chrome, but the largest exponent JS will convert to fixed notation in Chrome is e+20. After that, it reverts to always using scientific notation.
    Yes, that was what I intended to convey. The number is too big to be displayed other than in scientific notation.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    I'd start by using parseFloat to convert thoose strings to numbers - as that's what parseFloat is for.

    If you did that then numbers too big for JavaScript to handle would report as infinity rather than as zero.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

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