This article describes the method of PHP formatting MYSQL to return float type. Share it for your reference, as follows:
Get the float field of mysql in PHP. After echo output, the decimal part contains multiple 0s.
You can use floatval($num) to discard 0.
To retain decimal places, use number_format($num, 2);
The number_format function rounds up the value exceeding the specified number of digits.
If you don't want to round, keep all decimals. The following methods can be used:
// If you want to keep only two decimal places, you can use number_format($num, 2);echo f('1001.334534', 2) . '<br>'; // 1001.334534 echo f('-1001.000', 2) . '<br>'; // -1001.00 echo f('1001.3', 5) . '<br>'; // 1001.30000 echo f('1001.33') . '<br>'; // 1001.33 echo f('1001.000') . '<br>'; // 1001 // Format the decimals, but not round it. If there are decimals, it will be retained, and if there are no decimals, it will be added 0;function f($num, $v = 0) { $num = floatval($num); if ($v > 0) { $num = '' . $num; $arr = explode('.', $num); if (count($arr) === 1) { $num .= '.' . str_repeat('0', $v); } else { $v -= strlen($arr[1]); if ($v > 0) $num .= str_repeat('0', $v); } } return $num; }
For more information about PHP related content, please check out the topic of this site:Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Summary of php operation office documentation skills (including word, excel, access, ppt)》、《Summary of the usage of php date and time》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.