How to divide php by 100 with two decimal places: 1. Use the "/" operator to perform division operation, the syntax is "numeric value / 100"; 2. Use "number_format (division result, 2)" or " The sprintf("%.2f",division result)" statement rounds the value and retains two decimal places.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php divided by 100 Method of retaining two decimal places
In PHP, you can use the "/
" operator to perform division operations to divide a number by 100
<?php header("Content-type:text/html;charset=utf-8"); $a=332.5654; $b=100; $c=$a/$b; echo "一个数除以100的结果:".$c; ?>
It can be seen that if the dividend is a floating point number, its decimal point has several digits, so how to retain two decimal places?
Retain two decimal places: You can use the number_format()
or sprintf()
function to process the result of the division operation, retaining two decimal places.
number_format()
Numbers can be formatted by thousands grouping.
sprintf()
The function writes a formatted string into a variable.
<?php header("Content-type:text/html;charset=utf-8"); $a=332.5654; $b=100; $c=$a/$b; echo "一个数除以100的结果:".$c; echo "<br>使用number_format()保留两位小数:".number_format($c, 2); echo "<br>使用sprintf()保留两位小数:".sprintf("%.2f",$c); ?>
Note: The number_format() or sprintf() function will round when processing floating point numbers.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to divide by 100 in php with two decimal places. For more information, please follow other related articles on the PHP Chinese website!