Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Json and ajax 1 18
Html date format 9 20
15 puzzle in Javascript/html 2 33
assigning javascript variable to php variable 8 31
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…

679 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