The example in this article describes the way PHP formats MySQL to return the float type. Share to everyone for your reference, specific as follows:
PHP to obtain the MySQL float field, echo output, the decimal part is divided into more than 0.
You can use Floatval ($num) to go to 0 homes.
If you want to keep the decimal places, you can use Number_format ($num, 2);
The Number_format function is rounded up for a value that exceeds the specified number of digits.
If you do not want to be rounded, keep all decimals. You can use the following methods:
If you want to keep only two decimal places available 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 decimal, but not rounded, if there is a decimal is all reserved, no decimal is 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;
}
More interested in PHP related content readers can view the site topics: "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", " PHP Date and Time usage summary, PHP Introduction to object-oriented Programming, PHP String (String) Usage summary, PHP+MYSQL database Operations Tutorial and PHP Common database operating Skills summary
I hope this article will help you with the PHP program design.