I have such a set of data pseudo code (which is saved with strings now and will be converted at that time) {code ...} they have fixed decimal places. p is fixed and has a maximum of two valid digits after the decimal point, and n is a maximum of three valid digits after the decimal point. I need to calculate these numbers now. I have such a set of data...
Pseudocode (it is saved with a string and will be converted at that time)
"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 has a maximum of two valid digits after the decimal point, and n is a maximum of three valid digits after the decimal point.
I need to calculate these numbers now. I now know that computers are unreliable for floating-point computing. In addition, all the computing results I end up with only two to three valid digits after the decimal point, with very few division operations. most of them are addition and subtraction, even if there is division, I only keep the last three digits of the decimal point.
So I need to multiply all the numbers by 1000 so that all of them can be converted into integers, and continue the operation? Or can floating point numbers of this degree be directly used?
Reply content:
I have such a set of data
Pseudocode (it is saved with a string and will be converted at that time)
"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 has a maximum of two valid digits after the decimal point, and n is a maximum of three valid digits after the decimal point.
I need to calculate these numbers now. I now know that computers are unreliable for floating-point computing. In addition, all the computing results I end up with only two to three valid digits after the decimal point, with very few division operations. most of them are addition and subtraction, even if there is division, I only keep the last three digits of the decimal point.
So I need to multiply all the numbers by 1000 so that all of them can be converted into integers, and continue the operation? Or can floating point numbers of this degree be directly used?
There is no need. you only need to keep one decimal point. for example, if the currency uses three valid digits, only two valid digits are displayed. After all, if you still need to add/1000 after x1000, there is no difference at all. even if you only keep the shaping part and do not divide it (there is very little such a description of division and there is no value, you must consider the impact as long as there is ), it's useless.
The above is my opinion.
Use the toFixed method. Retain fixed decimal places. Then, click * 1.
var num=0.1+0.2;var result=num.toFixed(2)*1;console.log(result);// => 0.3