Solved

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

Posted on 2008-10-17
3
463 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

929 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

11 Experts available now in Live!

Get 1:1 Help Now