Javascript (js) decimal point multiplication and division _ javascript skills

Source: Internet
Author: User
This article mainly introduces the division of decimal points in javascript (js) in detail. If you need a friend, please refer to it and hope to help you. I. js computing
What are the results of 12.32*7? Answer: 86.24000000000001

Why is this problem? How can this problem be solved?
Js has a bug in processing the multiplication and division of decimals. The solution can be to convert decimals into integers.
The above calculation can be changed:
12.32x100x7/100
The result is: 86.24, correct.

In addition, calculate the following:
8.80x100x12/100
Result: 105.60000000000002
38.80 will also have similar problems.

Precision increased by 10 times:
8.80x1000x12/1000
Result: 105.6
Normal.

16.40x1000000x6/1000000
The result is also incorrect.

In order to make javascript execution more accurate, the value will be directly increased by 10000 times in subsequent JavaScript decimal calculations, and then divided by 10000 to solve the problem.
Var num = 38.80;
Var num2 = 13;
Alert (num x 10000*12/10000 );

The number to be multiplied and divided is the most suitable for tests of 10000. If the number is smaller, there is a problem. If the number is larger (1000000), there are also problems.

II,

The Code is as follows:


Related Article

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.