Multiplying floating point numbers in Javascript is a very interesting thing.
There are many ways to multiply floating point numbers. Here is a solution I gave that I think is good for myself:
Copy the code as follows: function FxF(f1, f2) {
f1 += '';
f2 += '';
var f1Len = f1.split('.')[1].length,
f2Len = f2.split('.')[1].length;
if (f1Len) {
f1 = f1.replace('.', '');
}
if (f2Len) {
f2 = f2.replace('.', '');
}
return f1 * f2 / Math.pow(10, f1Len + f2Len);
};
Basic ideas
The basic idea is to turn all floating point numbers into integers and then divide by the N-order power of 10 of the equal value. N is (sum of the following lengths of two floating point numbers).