The last time I talked about how to parse a large integer, a pen with number_format processing, and then read the function of the source code, the following is a small analysis.
Function prototypes
String Number_format (float $number [, int $decimals = 0]) string Number_format (float $number, int $decimals = 0, St Ring $dec _point = ".", string $thousands _sep = ",")
The function can accept 1, 2, 4 parameters (see the implementation of the code).
If only the first argument is provided, the number's decimal section is removed, and each thousand separator is the English lowercase comma ",";
If you provide two parameters, number will retain the digits after the decimal point to the value you set, the rest of the same upstairs;
If four parameters are supplied, number retains the fractional part of decimals length, the decimal point is replaced with Dec_point, and the thousand separator is replaced with THOUSANDS_SEP
Php_function (Number_format)
number//the number you want to format//num_decimal_places//the number of decimal places to keep//dec_separator//Specify the characters displayed by the decimals//thousands_separator//Specify the characters that the thousands separator displays /* {{proto string Number_format (float number [, int num_decimal_places [, String dec_separator, String Thousands_separat OR]]) Formats a number with grouped thousands */php_function (Number_format) {//expected Number_format first parameter num is double type, in Word The FA phase has been converted to literal constants by double num; Zend_long Dec = 0; Char *thousand_sep = NULL, *dec_point = NULL; Char thousand_sep_chr = ', ', DEC_POINT_CHR = '. '; size_t Thousand_sep_len = 0, Dec_point_len = 0; Parse parameter Zend_parse_parameters_start (1, 4) z_param_double (num)//Gets the DOUBLE type of num z_param_optional z_ Param_long (Dec) z_param_string_ex (Dec_point, Dec_point_len, 1, 0) z_param_string_ex (thousand_sep, thousand_s Ep_len, 1, 0) zend_parse_parameters_end (); Switch (Zend_num_args ()) {Case 1:return_str (_php_math_number_format (NUM, 0, DEC_POINT_CHR, THOUSAND_SEP_CHR)); Break Case 2:RETURN_STR (_php_math_number_format (num, (int) Dec, DEC_POINT_CHR, THOUSAND_SEP_CHR)); Break Case 4:if (Dec_point = = NULL) {dec_point = &dec_point_chr; Dec_point_len = 1; } if (thousand_sep = = NULL) {thousand_sep = &thousand_sep_chr; Thousand_sep_len = 1; }//_PHP_MATH_NUMBER_FORMAT_EX//real processing of the function, RETVAL_STR (_PHP_MATH_NUMBER_FORMAT_EX (num) In this file, line 1107th (int ) Dec, Dec_point, Dec_point_len, Thousand_sep, Thousand_sep_len)); Break Default:wrong_param_count; }}/* }}} */
Code Execution flowchart
_php_math_number_format_ex
The number of parameters implemented by the function will eventually call the _PHP_MATH_NUMBER_FORMAT_EX function. The main functions of the function are:
handle negative numbers;
The floating-point number is rounded according to the decimal point to be retained;
Call the strpprintf function to turn the floating-point expression into a string representation;
Calculates the length of the string that needs to be assigned to the result variable;
Copy the result to the return value (if there is a thousand character, the thousand character is split)
strpprintf
This function is to implement the conversion of floating-point numbers and strings, as mentioned above, finally called the PHP_CONV_FP function to do the conversion (here is the location through GDB debugging), while the PHP_CONV_FP function, tracking down, the call is Zend_dtoa function,
See the GitHub Project submission notes for more details.
Summarize
After reading this function's source code, learned is the floating number and the string conversion implementation details, the string and the floating-point relationship is more complex, and then continue to learn.