I have such a set of data
Pseudo-code (now saved with a string, then converted)
"buy": [{ "p": "1698.64000", "n": "0.117" }, { "p": "1698.63000", "n": "5.000" }, { "p": "1696.54000", "n": "0.128" }, { "p": "1696.34000", "n": "0.176"
They have fixed decimal places, p is fixed and there are up to 2 significant digits after the decimal point, and N is a maximum of 3 digits after the decimal point.
I need to do some calculations on these numbers now. I now understand that the computer is not reliable for floating point calculation, and all of the results I end up as long as the decimal point 2 to 3 valid digits, the division is very few, most of the multiplication, even if there is division of the result I will only keep the decimal point after 3 bits.
So I need to multiply all the numbers by 1000 so that they all become integral types, and then proceed with the operation? Or is this a level of floating point that can be used directly?
Reply content:
I have such a set of data
Pseudo-code (now saved with a string, then converted)
"buy": [{ "p": "1698.64000", "n": "0.117" }, { "p": "1698.63000", "n": "5.000" }, { "p": "1696.54000", "n": "0.128" }, { "p": "1696.34000", "n": "0.176"
They have fixed decimal places, p is fixed and there are up to 2 significant digits after the decimal point, and N is a maximum of 3 digits after the decimal point.
I need to do some calculations on these numbers now. I now understand that the computer is not reliable for floating point calculation, and all of the results I end up as long as the decimal point 2 to 3 valid digits, the division is very few, most of the multiplication, even if there is division of the result I will only keep the decimal point after 3 bits.
So I need to multiply all the numbers by 1000 so that they all become integral types, and then proceed with the operation? Or is this a level of floating point that can be used directly?
There is no need, as long as the number of 1 decimal places, such as currency using 3 digits, only in the display of the format as a valid number of 2 digits can be. After all, after you x1000 if you want to/1000 later, it is no different, even if you only retain the plastic part is not divisible (division is very few this description is not worth, as long as there is a need to consider the impact), just as useless.
These are personal views.
Use the Tofixed method. retains fixed decimal digits. And then * * can.
var num=0.1+0.2;var result=num.toFixed(2)*1;console.log(result);// => 0.3