![](/img/trans.png)
[英]How to get the ceil value after 2 decimal places using JavaScript
[英]how to get customised ceil and floor value for a given variable?
我想使用javascript自定義ceil和floor。 例如:如果特定的浮點變量10.1,那么我需要將結果作為10(底),如果該值大於10.1,則需要將其設為11(上限)。 這里10是可變的。 誰能給我解決方案?
最好用數學運算,而不要加上小數。 減去整數,檢查余數,如果大於.1,則更新整數並將其返回。
function customRound (num) { let wn = Math.floor(num) let adj = num - wn > .1 ? 1 : 0 return wn + adj } console.log('10.1', customRound(10.1)) console.log('10.11', customRound(10.11)) console.log('10.01', customRound(10.1)) console.log('10.100000000001', customRound(10.100000000001))
這是一種方法:
Math.round(thing + 0.4);
假設事物= 2.11,那么您將舍入2.51 =3。如果事物= 2.09,您將舍入2.49 =2。應該可以!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.