Solved

Can I override the default * function in javascript with a more accurate multiply?

Posted on 2008-10-17
3
459 Views
Last Modified: 2013-11-11
I've recently noticed how the default multiplication in javascript uses floats, which, in something like the following  .7 * 1.3 * .5 = .45499999999999 which when rounded to 2 decimals = .45 instead of .46 which is what it should be if you use a calculator...  

Is it possible to replace/override the math functions so that when you do something like .7 * 1.3 it automatically multiplies it by 1000 and divides by 1000 then again when it multiplies by .5 to insure that the decimals are always kept exact?

Ideally I'm looking for a solution that fixes potentially over a 100 calls in a large app without having to go through each function and fixing them.
0
Comment
Question by:dndorksadmin
3 Comments
 
LVL 10

Accepted Solution

by:
GeoffSutton earned 500 total points
ID: 22746112
Take a look at http://www.mozilla.org/js/language/js20-2000-07/libraries/operator-overloading.html

It is looking like overriding operators is not fully implemented in Javascript.  This outlines an option library that can be installed, but may not work on a wider basis.  There is a proposal for Javascript2 to implement this, but several security arguments against.  See http://ajaxian.com/archives/operator-overloading-in-javascript-2-and-a-potential-monster-csrf-hole
There is a blog referenced in there about the actual security leak.  However, currently there is no means of doing it.  Your best bet is to create a function and use that in place of the operator.

Good luck on this,

Geoff
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22747103
There is a VERY easy way to do this which hardly anyone thinks of.  Multiply all values by one million, then multiply those values, and thentake the product of the multiplication, and process it yourself down to as many decimal places as you want, limited by 12 to 20 decimals.  Don't use built in rounding functions, search the string to the decimal places you want, PLUS three, and make your own decision on rounding.  Too easy!
0
 

Author Comment

by:dndorksadmin
ID: 22748634
Actually scrathcyboy, thats why I wanted to override the multiplication function.  Wouldn't it be awesome to change all the * to multiply everything by a million then divide back by the million rounded however I want for the entire site?  Then we could write standard javascript but have the site accurate to the level we wanted.  So anyways, the solution to accurate javascript is easy, the solution to fixing an entire intranet in one fell swoop is less so. :(
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now