Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work around the computation limitation of JavaScript

Tags:

javascript

Run this snippet

            document.write(parseFloat("999999999999999"));
            document.write("<br>");
            document.write(parseFloat("555555555555555555555555.99"));
            document.write("<br>");
            document.write(parseInt("9999999999999999.99"));
    
            document.write("<br>");
            document.write(parseInt("88888888888888888",10));
            document.write("<br>");
            document.write(parseFloat("88888888888888888"));

You will notice that the conversion from string to a valid number is buggy. This is a known limitation in javascript up to 15 digits(?). I know that if it is a JS limitation then we can't do anything about it. But in the ajax enterprise world, we will always need to compute lots in the browser. And with the advent of node.js in the server-side, this is no longer tolerable and the community need to find a solution to circumvent, if not solve, this problem once and for all.

My boss found a high precision Javascript calculator but it is cpu hogging. Another solution is to give that to the server-side but that will increase "chattiness" which is an anti-pattern. What I am asking is, how would you solve this?

Please correct me If I am wrong in any of the things I stated above.

like image 233
Eugene Ramirez Avatar asked Sep 15 '10 00:09

Eugene Ramirez


1 Answers

If JavaScript can't parse huge numbers, chances are it won't be able to do anything useful with them either, like calculations (without the memory-intensive calculator you posted a link to). If you need to display the number, why not use an Ajax request? Just try to consolidate multiple number-to-string operations into a single request.

like image 76
Chris Laplante Avatar answered Sep 30 '22 14:09

Chris Laplante