Phpfloat is a summary of the float string method. Php intercepts the floating point type in the following ways: 1. floatround (float $ val [, int $ precision]) returns the number of values after the decimal point based on the specified precision (number of digits after the decimal point ).
There are several methods to intercept floating point in php:
1,Float round (float $ val [, int $ precision]) returns the result of rounding val according to the specified precision (number of digits after decimal point. Precision can also be negative or zero (default ).
Echo round (4.3) // 4
2,String sprintf (string $ format [, mixed $ args [, mixed $...]) returns the formatted data string
The code is as follows:
$ A = 12.338938438;
Echo sprintf ("%. 5f", $ a) // Result: 12.33894
$ A = 12.3312356;
Echo sprintf ("%. 5f", $ a); // 12.33124
Echo sprintf ("% f", $ a); // 331236 the default value is 6 digits after the decimal point
3,String number_format (float $ number, int $ decimals, string $ dec_point, string $ thousands_sep)
The code is as follows:
$ Number = 1234.5678;
$ English_format_number = number_format ($ number, 2 ,'.','');
Echo $ english_format_number; // 1234.57
All of the above are automatically rounded down. sometimes the demand does not need to be rounded down. what should I do? I didn't think of a good solution. who knows I can tell you.
I wrote a troublesome function and recorded it.
The code is 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
Round 1, float round (float $ val [, int $ precision]) returns the number of values after the decimal point based on the specified precision (number of digits after the decimal point...