This article will introduce you to the detailed explanation of PHP floating point precision value example program. I hope that friends who need to understand floating point precision value can refer to it.
The precision value of floating point numbers in PHP is used to control the output of the floating point number. It can be understood as controlling the number of output digits. Different precision values may result in different output results. Note: Internally, it is still in accordance with The actual value is stored. When two floating point numbers are used for four arithmetic operations, their original values are still used.
Precision is used in the configuration file of PHP to set the precision value of the globally specified floating point number. It seems that the default settings are different in each distribution. I saw it under window and saw it as 12 under Linux. This value is 14. Of course, you can also use ini_set in the program to change the global settings, for example:
代码如下 |
复制代码 |
ini_set("precision", "15"); |
I have always understood precision as the number of decimal points retained. Is this the case in PHP floating point numbers? The answer is no.
Floating point numbers are actually composed of an integer part and a decimal part. The precision here means that the number of digits in the integer part plus the number of digits in the decimal part cannot exceed the maximum precision value. If it exceeds, it will be truncated to the maximum precision according to the rounding method. value. If the integer part is 0, the number of digits will not be counted, and the 0 at the end of the decimal part will not be counted. In addition, for the same number, different precision may display different forms of expression. The following is explained by way of example.
Case in which the integer part is 0
The code is as follows |
Copy code |
代码如下 |
复制代码 |
$num = 0.12345600000000000;
//整数部分为0 ,位数为 0 ,小数部分末尾的 0 不计入位数,所以总位数为 6
ini_set("precision", "12");
echo $num; // 0.123456
//未超过精度值,显示的结果为 0.123456
ini_set("precision", "3");
echo $num; // 0.123
//超过精度值,保留3位
ini_set("precision", "5");
echo $num; // 0.12346
|
$num = 0.12345600000000000;
//The integer part is 0, the number of digits is 0, the 0 at the end of the decimal part is not included in the number of digits, so the total number of digits is 6
ini_set("precision", "12");
echo $num; // 0.123456
//The precision value is not exceeded, the displayed result is 0.123456
ini_set("precision", "3");
echo $num; // 0.123
//Exceed the precision value, retain 3 digits
代码如下 |
复制代码 |
$num = 12.12345600000000000;
//整数部分为12 ,位数为 2 ,小数部分末尾的 0 不计入位数,位数为6,所以总位数为 2 + 6
ini_set("precision", "12");
echo $num; // 12.123456
//未超过精度值,显示的结果为 12.123456
ini_set("precision", "3");
echo $num; // 12.1
//超过精度值,整数部分位数为 2 ,所以只保留一位小数
ini_set("precision", "5");
echo $num; // 12.123
//超过精度值,整数部分位数为 2 ,所以只保留3位小数可以看到小数点后保留的位数跟精度已经整数部分的位数有关
|
ini_set("precision", "5");
echo $num; // 0.12346
|
//Exceed the precision value, retain 5 digits. In this case, the precision value is equivalent to the number of digits retained after the decimal point.
Case in which the integer part is greater than 0
The code is as follows |
Copy code |
$num = 12.12345600000000000;
//The integer part is 12, the number of digits is 2, the 0 at the end of the decimal part is not included in the number of digits, the number of digits is 6, so the total number of digits is 2 + 6
ini_set("precision", "12");
echo $num; // 12.123456
//The precision value is not exceeded, the displayed result is 12.123456
ini_set("precision", "3");
echo $num; // 12.1
//Exceeding the precision value, the number of digits in the integer part is 2, so only one decimal place is retained
ini_set("precision", "5");
echo $num; // 12.123
//Exceeding the precision value, the number of digits in the integer part is 2, so only 3 decimal digits are retained. You can see that the number of digits retained after the decimal point is related to the precision and the number of digits in the integer part
|
.
Case 2 when the integer part is greater than 0
The code is as follows
代码如下 |
复制代码 |
$num = 12345678.12345600000000000;
//整数部分为12345678 ,位数为 8 ,小数部分末尾的 0 不计入位数,位数为6,所以总位数为 8 + 6
ini_set("precision", "12");
echo $num; // 12345678.1235
//超过精度值,显示的结果为 12345678.1235
ini_set("precision", "3");
echo $num; // 1.23E+7
//超过精度值,且整数部分位数超过精度,小数部分舍弃,且整数部分只取3位
ini_set("precision", "5");
echo $num; // 12346000
|
|
Copy code
|
$num = 12345678.12345600000000000;
//The integer part is 12345678, the number of digits is 8, the 0 at the end of the decimal part is not included in the number of digits, the number of digits is 6, so the total number of digits is 8 + 6
代码如下 |
复制代码 |
$num1 = 1331625729.687;
$num2 = 1331625730.934;
ini_set("precision", "8");
echo $num1 . '
';
echo $num2 . '
';
$sub = $num1 - $num2;
echo $sub . '
';
//输出的结果为:
/*
1331625700
1331625700
-1.247
*/
|
ini_set("precision", "12");
echo $num; // 12345678.1235
//Exceeding the precision value, the displayed result is 12345678.1235
ini_set("precision", "3");
echo $num; // 1.23E+7
//The precision value is exceeded, and the number of digits in the integer part exceeds the precision, the decimal part is discarded, and only 3 digits are taken for the integer part
ini_set("precision", "5");
echo $num; // 12346000
代码如下 |
复制代码 |
$a = 1315537636.338467;
printf("%f", $a); echo "n";
echo $a . "n";
echo $a; echo "n";
?>
结果
1315537636.338467
1315537636.3385
1315537636.3385
|
|
//Exceeds the precision value, and the number of digits in the integer part exceeds the precision, the decimal part is discarded, and the integer part only takes 5 digits. As you can see from the above example, the precision value is also related to the interception of the integer part. Notice that the last two examples are displayed in different ways, one is using scientific notation, and the other is followed by 0 complement. The conclusion drawn through experiments is that when the number of digits in the integer part minus the precision value is greater than 4, scientific notation is used, otherwise it is supplemented with 0. In other words, when the number of digits in the integer part exceeds the precision value, it is truncated. Finally, the number of padded zeros will not exceed 4.
Floating point operations
The code is as follows
Copy code
|
$num1 = 1331625729.687;
$num2 = 1331625730.934;
ini_set("precision", "8");
echo $num1 . '
';
echo $num2 . '
';
$sub = $num1 - $num2;
echo $sub . '
';
//The output result is:
/*
1331625700
1331625700
-1.247
*/
The above example shows that the precision value only controls the display result, and the internal storage is still the original value, so the value of $sub is 1331625729.687 minus 1331625730.934.
The code is as follows
|
Copy code
|
<🎜> $a = 1315537636.338467;<🎜>
<🎜> printf("%f", $a); echo "n";<🎜>
<🎜> echo $a . "n";<🎜>
<🎜> echo $a; echo "n";<🎜>
<🎜> ?>
Results
1315537636.338467
1315537636.3385
1315537636.3385
In other words, it is not possible to use the most convenient method of PHP to convert floating point numbers into strings or display them. You must use printf/sprintf to convert floating point numbers into strings.
http://www.bkjia.com/PHPjc/632694.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632694.htmlTechArticleThis article will introduce to you the detailed explanation of the PHP floating point precision value example program. I hope you need to understand floating point numbers. Friends who know the accuracy value can enter for reference. The precision value of floating point numbers in php is...
|
|
|
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn