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