el-input value precision loss problem: el-inpu value * 100 unit conversion appears many decimal places

This requirement comes from a search function. The unit of data on the page is m, and the server receives it in cm. It was originally very simple to send *100 to the server. As a result, the test students found the following ghost bug :

 

As we all know: 0.1+0.2!==0.3 in js, because js uses double- precision floating point, so there will be errors in the encoding of data stored in the computer;

 

The requirement in this issue is to allow the user to enter two decimal places. In the project, there are custom commands that limit the input to only two places. When troubleshooting, it is found that only when the two decimal places after the input are greater than 5, the ghost bug above will appear. For example (15.99) and the like, (15.33) will not have this kind of problem..., I was eager to go online and solved the problem with Math.round(value * 100) .

Then record a method that retains two decimal places:

fomatFloat(src) {
      return Math.round(src * Math.pow(10, 2)) / Math.pow(10, 2);
    }

The number 2 above is the number of digits, which can be raised and repackaged, and how many decimal digits need to be kept and transmitted.

Simply memorize it, and then fix the BUG.

Guess you like

Origin blog.csdn.net/m0_58481462/article/details/126585030