JavaScript Math vs Excel

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sasuke

    #16
    Re: JavaScript Math vs Excel

    On Oct 31, 2:57 am, Conrad Lender <crlen...@yahoo .comwrote:
    On 2008-10-30 21:59, Evertjan. wrote:
    >
    >
    >
    Wow, not that easy in regex, try:
    >
    <script type='text/javascript'>
    >
    function round3dec(x) {
      x = Math.floor(x*10 00+.5)+'';
      var s = x.replace(/(^\-?).*/,'$1');
      x = x.replace(/^\-?/,'0000');
      return s + (1*x.replace(/\d\d\d$/,'')) + '.' +
        x.replace(/-?\d*(\d\d\d)$/,'$1');
    };
    >
    alert( round3dec(21.23 45678) );
    alert( round3dec(-21.2345678) );
    alert( round3dec(0.002 5555) );
    alert( round3dec(-0.0025555) );
    >
    </script>
    >
    (10000000000000 00000).toFixed( 3)
      -100000000000000 0000.000
    >
    round3dec(10000 00000000000000)
      -1e+21.00001e+21
    >
    :-)
    LOL, that's a nice one. Moral of the story: Use built-in functions
    whenever possible. ;-)

    ../sasuke

    Comment

    • Evertjan.

      #17
      Re: JavaScript Math vs Excel

      sasuke wrote on 31 okt 2008 in comp.lang.javas cript:
      On Oct 31, 2:57 am, Conrad Lender <crlen...@yahoo .comwrote:
      >On 2008-10-30 21:59, Evertjan. wrote:
      >>
      >>
      >>
      Wow, not that easy in regex, try:
      >>
      <script type='text/javascript'>
      >>
      function round3dec(x) {
        x = Math.floor(x*10 00+.5)+'';
        var s = x.replace(/(^\-?).*/,'$1');
        x = x.replace(/^\-?/,'0000');
        return s + (1*x.replace(/\d\d\d$/,'')) + '.' +
          x.replace(/-?\d*(\d\d\d)$/,'$1');
      };
      >>
      alert( round3dec(21.23 45678) );
      alert( round3dec(-21.2345678) );
      alert( round3dec(0.002 5555) );
      alert( round3dec(-0.0025555) );
      >>
      </script>
      >>
      >(1000000000000 000000).toFixed (3)
      >  -100000000000000 0000.000
      >>
      >round3dec(1000 000000000000000 )
      >  -1e+21.00001e+21
      >>
      >:-)
      >
      LOL, that's a nice one. Moral of the story: Use built-in functions
      whenever possible. ;-)
      Not at all, there is no joy in using build-in functions,
      but for those who love production.

      The above example of conrad is out of bounds in normal use.

      Regex is a joyful puzzle by itself.


      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      • Dr J R Stockton

        #18
        Re: JavaScript Math vs Excel

        In comp.lang.javas cript message <ca9917a8-230f-48df-a016-1efe5081a3fa@i1
        8g2000prf.googl egroups.com>, Fri, 31 Oct 2008 07:15:36, sasuke
        <database666@gm ail.composted:
        >
        >LOL, that's a nice one. Moral of the story: Use built-in functions
        >whenever possible. ;-)
        >
        But only where they are trustworthy on "all" systems. Method toFixed
        has errors in IE.

        --
        (c) John Stockton, near London. *@merlyn.demon. co.uk/?.?.Stockton@ph ysics.org
        Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
        Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
        Do not Mail News to me. Before a reply, quote with ">" or "" (SoRFC1036)

        Comment

        Working...