要顯示兩位小數,請使用 number_format()。讓我們先編寫相同的 PHP 程式碼。我們先宣告兩個變數被初始化-
$number1=10.3423; $number2=10;
現在,使用number_format()函數顯示兩個小數−
$result1=number_format ($number1, 2); $result2=number_format ($number2, 2);
以下是範例−
#<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>
以下是PHP程式碼的快照−
10.34 10.00
以上是如何在MySQL/PHP中顯示兩位小數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!