JavaScript-do I need to use a finite repeating decimal with a decimal point multiplied by 10 N to avoid floating point problems?

Source: Internet
Author: User
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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.