簡體   English   中英

Javascript:將邊界0.5的小數點后兩位取整

[英]Javascript : Rounding 2 decimal places of boundary 0.5

如何獲得四舍五入數0.5,之前我在此鏈接中找到過:

點擊這里

但這不是我想要的:

我想要這個例子:

  1. 10.24 = 10.25
  2. 17.90 = 17.90
  3. 3.89 = 3.90
  4. 7.63 = 7.65

jQuery對我很好。

如果您想將數字四舍五入到最接近的0.05 ,請使用Math.round(num * 20) / 20

這將滿足您的需求...

function roundoff(value) {
    return (Math.round(value / 0.05) * 0.05).toFixed(2);
}

使用Math.round會將值四舍五入至最接近的0.05,然后使用toFixed(2)將確保結果具有2個小數位,即使它們都是零。

這是一個可行的例子...

http://jsfiddle.net/xoo7c898/

暫無
暫無

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

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