Home > Article > Backend Development > How to convert int (integer) type to decimal in php
Conversion method: 1. Use the number_format() function, the syntax "number_format (int type value, number of decimal places)"; 2. Use the sprintf() function, the syntax "sprintf("%.xf", value of type int)", the parameter x is the number of decimal places.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php will int ( Integer) type to decimal
Method 1: Use the number_format() function
number_format() can format numbers by thousands grouping .
<?php echo number_format(12,1)."<br>"; echo number_format(12,2)."<br>"; echo number_format(12,3)."<br>"; echo number_format(12,4)."<br>"; ?>
Method 2: Use sprintf() function
sprintf() function writes the formatted string into a variable middle.
<?php echo sprintf("%.1f",12)."<br>"; echo sprintf("%.2f",13)."<br>"; echo sprintf("%.3f",14)."<br>"; echo sprintf("%.4f",15)."<br>"; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert int (integer) type to decimal in php. For more information, please follow other related articles on the PHP Chinese website!