Home >Backend Development >PHP Problem >What are the methods to determine the exact number of decimal places in PHP?
php Method for decimal precision: 1. Use sprintf() function to format, syntax "sprintf("%.decimal digits f",$num)"; 2. Use number_format() function , syntax "number_format($num,'number of decimal places')".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php decimal precision decimal point Method for the last digit
Method 1: Use sprintf to format the string
<?php header("Content-type:text/html;charset=utf-8"); $num = 10.4567; $format_num = sprintf("%.2f",$num); echo $format_num; //10.46 ?>
Method 2: Use the number_format() function
number_format(): The function can format numbers by grouping thousands.
Syntax:
number_format(number,decimals,decimalpoint,separator)
Parameters:
number: required. The number to format.
decimals: Optional. Specify how many decimal points
decimalpoint: Optional. Specifies the string used as the decimal point.
separator: Optional. Specifies the string used as thousands separator.
Example:
<?php header("Content-type:text/html;charset=utf-8"); $num = 10.4567; $format_num = number_format($num,'1'); echo $format_num."<br>"; $format_num = number_format($num,'2'); echo $format_num."<br>"; $format_num = number_format($num,'3'); echo $format_num; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the methods to determine the exact number of decimal places in PHP?. For more information, please follow other related articles on the PHP Chinese website!