Javascript-do I need to multiply the finite, non-repeating decimal places with the decimal point by the nth power of 10 and then use it again to avoid floating point problems?

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

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.