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

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

WBOY
發布: 2023-09-02 08:42:01
轉載
1014 人瀏覽過

如何在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
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板