There are several ways to intercept floating-point types in PHP:
1, float round (float $val [, int $precision]) Returns the result of rounding Val based on the specified precision precision (number of digits after decimal decimal point). Precision can also be negative or 0 (the default value).
echo round (4.3)//4
2 . String sprintf (String $format [, Mixed $args [, mixed $ ...]) returns the formatted data strings
Copy the Code code as follows:
$a = 12.338938438;
Echo sprintf ("%.5f", $a)//results: 12.33894
$a = 12.3312356;
Echo sprintf ("%.5f", $a);//12.33124
Echo sprintf ("%f", $a);//331236 6 digits after the default decimal point
3, string Number_format (float $number, int $decimals, String $dec _point, string $thousands _sep)
Copy the Code code as follows:
$number = 1234.5678;
$english _format_number = Number_format ($number, 2, '. ', ');
echo $english _format_number; 1234.57
All of these are automatically rounded, sometimes demand does not need to be rounded up, how to do, did not think of a good way, who know can tell a sound.
I wrote a trouble-point function and recorded it.
Copy the Code code as follows:
function Getfloatvalue ($f, $len)
{
$tmpInt =intval ($f);
$tmpDecimal = $f-$tmpInt;
$str = "$tmpDecimal";
$subStr =strstr ($str, '. ');
if (strlen ($SUBSTR) < $len + 1)
{
$repeatCount = $len +1-strlen ($SUBSTR);
$str = $str. "". Str_repeat ("0", $repeatCount);
}
return $tmpInt. "". substr ($str, 1,1+ $len);
}
echo Getfloatvalue (12.99,4)//12.9900
echo Getfloatvalue (12.9232555553239,4)//12.9232
http://www.bkjia.com/PHPjc/824906.html www.bkjia.com true http://www.bkjia.com/PHPjc/824906.html techarticle There are several ways to intercept floating-point types in PHP: 1, float round (float $val [, int $precision]) returns VAL based on the specified precision precision (number of digits after decimal decimal point ...