簡體   English   中英

如果小數點后有一個零,則加一個零

[英]add one zero if after decimal there is one zero

我有一種定價格式,其中有許多場景需要處理!

我的基本要求是小數點后必須有兩個值,例如

20.20
100.03
30.34

等等,現在我收到的定價類型是

30.3
25.52 
100.02

等等

現在我要構建的邏輯是,如果小數點后有兩個值,則應按原樣顯示它們,而無需任何更改。 但是,如果小數點后有一個值(例如30.3 ,則應添加尾隨零,但最多只能增加兩位數! 這就是我現在所擁有的!

  <h6> <?php $starting_from = explode('.',$result_1s['price']) ?>
  $<?=round($starting_from[0])?><?=(is_numeric($result_1s['price']) && floor($result_1s['price']) != $result_1s['price']) ? '.'.$starting_from[1] :'.00' ?>
 </h6>

我如何才能修改上面的代碼,使其接受我上面說明的方案。 提前致謝!

您可以像這樣使用number_format()

number_format($result_1s['price'], 2, '.', '');

文獻資料

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM