首頁 >後端開發 >php教程 >如何在MySQL/PHP中顯示兩位小數?

如何在MySQL/PHP中顯示兩位小數?

WBOY
WBOY轉載
2023-09-02 08:41:111020瀏覽

如何在MySQL/PHP中顯示兩位小數?

要顯示兩位小數,請使用 number_format()。讓我們先編寫相同的 PHP 程式碼。我們先宣告兩個變數被初始化-

$number1=10.3423;
$number2=10;

現在,使用number_format()函數顯示兩個小數−

$result1=number_format ($number1, 2);
$result2=number_format ($number2, 2);

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">&#39;<br>&#39;</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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除

相關文章

看更多