Phpround problem (incorrect precision result)

Source: Internet
Author: User
Phpround problems (incorrect precision results) php

It is found that there is a problem when using the PHP round function for decimal control, as shown below:

Expected value of round (9999999999999.99, 2): 9999999999999.99; actual value: 10000000000000
Expected value of round (1111111111111.11, 2): 1111111111111.11; actual value: 1111111111111.1

Have you encountered any similar problems? Is this a known defect in PHP? No official descriptions are available.

Note: The PHP version on my machine is 5.3.6.


Reply to discussion (solution)

The round function is correct.

$a = 1111111111111.11;echo $a;

Refer to the introduction to the floating point type precision in php.
Http://www.php.net/manual/zh/language.types.float.php

The round function is correct.

$a = 1111111111111.11;echo $a;

Refer to the introduction to the floating point type precision in php.
Http://www.php.net/manual/zh/language.types.float.php

$ A = 1111111111111.11;
Echo $;
What does this code describe? Didn't call round?


The round function is correct.

$a = 1111111111111.11;echo $a;

Refer to the introduction to the floating point type precision in php.
Http://www.php.net/manual/zh/language.types.float.php

$ A = 1111111111111.11;
Echo $;
What does this code describe? Didn't call round?

The output is 1111111111111.1, which indicates that 1111111111111.11 is converted to 1111111111111.1 during script compilation.
It may be because the floating point type of php cannot be stored for 1111111111111.11 due to precision reasons.
Have you read the official php instructions? "So never believe that the floating point result is accurate to the last digit"



The round function is correct.

$a = 1111111111111.11;echo $a;

Refer to the introduction to the floating point type precision in php.
Http://www.php.net/manual/zh/language.types.float.php

$ A = 1111111111111.11;
Echo $;
What does this code describe? Didn't call round?

The output is 1111111111111.1, which indicates that 1111111111111.11 is converted to 1111111111111.1 during script compilation.
It may be because the floating point type of php cannot be stored for 1111111111111.11 due to precision reasons.
Have you read the official php instructions? "So never believe that the floating point result is accurate to the last digit"


What should I do if I want to display all the figures I have mentioned?




The round function is correct.

$a = 1111111111111.11;echo $a;

Refer to the introduction to the floating point type precision in php.
Http://www.php.net/manual/zh/language.types.float.php

$ A = 1111111111111.11;
Echo $;
What does this code describe? Didn't call round?

The output is 1111111111111.1, which indicates that 1111111111111.11 is converted to 1111111111111.1 during script compilation.
It may be because the floating point type of php cannot be stored for 1111111111111.11 due to precision reasons.
Have you read the official php instructions? "So never believe that the floating point result is accurate to the last digit"


What should I do if I want to display all the figures I have mentioned?
You still haven't read the official instructions. The instructions are clear and you can find the answer yourself.





The round function is correct.

$a = 1111111111111.11;echo $a;

Refer to the introduction to the floating point type precision in php.
Http://www.php.net/manual/zh/language.types.float.php

$ A = 1111111111111.11;
Echo $;
What does this code describe? Didn't call round?

The output is 1111111111111.1, which indicates that 1111111111111.11 is converted to 1111111111111.1 during script compilation.
It may be because the floating point type of php cannot be stored for 1111111111111.11 due to precision reasons.
Have you read the official php instructions? "So never believe that the floating point result is accurate to the last digit"


What should I do if I want to display all the figures I have mentioned?
You still haven't read the official instructions. The instructions are clear and you can find the answer yourself.

OK, thanks

Floating-point numbers can only save values with limited digits.

Php has provided high-precision mathematical functions library BC, GMP

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.