To display two decimal places, use number_format(). Let's start by writing the same PHP code. We first declare two variables to be initialized-
$number1=10.3423; $number2=10;
Now, use the number_format() function to display two decimals-
$result1=number_format ($number1, 2); $result2=number_format ($number2, 2);
The following is an example-
<span class="pun"><?</span><span class="pln">php</span> <span class="pln"> $number1</span><span class="pun">=</span><span class="lit">10.3423</span><span class="pun">;</span> <span class="pln"> $number2</span><span class="pun">=</span><span class="lit">10</span><span class="pun">;</span> <span class="pln"> $result1</span><span class="pun">=</span><span class="pln">number_format </span><span class="pun">(</span><span class="pln">$number1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">);</span> <span class="pln"> $result2</span><span class="pun">=</span><span class="pln">number_format </span><span class="pun">(</span><span class="pln">$number2</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">);</span> <span class="pln"> echo </span><span class="pun">(</span><span class="pln">$result1</span><span class="pun">);</span> <span class="pln"> echo </span><span class="str">'<br>'</span><span class="pun">;</span> <span class="pln"> echo </span><span class="pun">(</span><span class="pln">$result2</span><span class="pun">);</span> <span class="pun">?></span>
The following is a snapshot of the PHP code−
10.34 10.00
The above is the detailed content of How to display two decimal places in MySQL/PHP?. For more information, please follow other related articles on the PHP Chinese website!