簡體   English   中英

Javascript | 浮點數+區間循環

[英]Javascript | Floating point numbers + interval loop

我有一個浮點數(例如):0.004178174922295

我怎樣才能讓遞減函數在特定的時間(例如 1 秒)內從這個數字到 0 進行計算? 謝謝。

預期值:

0.004178174922295
0.004178174922294
0.004178174922293
...
0

正如raina77ow和其他人在評論中指出的那樣, 在JS中使用十進制數的操作是有問題的,進行了近似,結果可能不准確

一種解決方法是將無窮小數轉換為大整數,使用它們並在最后將它們轉換回來。

這就是你要找的嗎? 請告訴我。

編輯您可以要求在一定時間內完成倒計時,它確實可以使用合理的數字,但是在 Javascript 中,最小間隔為 10 毫秒,您不能調用比這更短的間隔。 使用您給出的示例數字0.004178174922295 ,這就像從4178174922295到零。 這將需要以 10 毫秒為間隔幾乎 1325 年(如果我的數學是正確的,無論哪種方式,我都希望您會通過更短的時間間隔)。

 function infinitesimalCountDown(num, seconds) { // I create a coeficient to convert the decimal to an int // Will be a big number starting with "1" and followed by a bunch of zeroes let coefString = '1'; for(let i=0; i<num.toString().length-2; i++) { coefString += '0'; } const coef = Number(coefString); // This has the digits from the original original but it's an int let counter = Math.round(num*coef); // Now I can just treat it as an int and convert it back for the output const icdInterval = setInterval(() => { counter--; console.log(counter/coef); if(counter <= 0) clearInterval(icdInterval); }, Math.round(seconds*1000/counter)); } console.log("It works with a short number"); infinitesimalCountDown(0.0041, 10); setTimeout(() => { console.log("It doesn't work with a long number"); infinitesimalCountDown(0.004178174922295, 3000); }, 12 * 1000);

如果您對 Javascript 能夠處理它所必需的步驟感到滿意,則可以執行以下操作:

 function infinitesimalCountDown(num, seconds) { let coefString = '1' for(let i=0; i<num.toString().length-2; i++) { coefString += '0' } const coef = Number(coefString) let counter = Math.round(num*coef) let steps = seconds * 1000 / counter steps = steps < 100 ? 100 : steps let step = 1 if(steps == 100) { step = counter / ((seconds * 1000) / steps) } console.log(step) const icdInterval = setInterval(() => { counter -= step; if(counter <= 0) { counter = 0 clearInterval(icdInterval) } console.log(counter/coef) }, steps) } infinitesimalCountDown(0.004178174922295, 5)

如果您可以將輸入數字表示為number類型(因此沒有很多小數),您可以使用普通的數字減法來做到這一點。

在這里,重要的是得到要減去的單位。 您可以使用Math.pow獲取單位。

從這個浮點指南中,需要對計數的數字進行四舍五入,這可以使用toFixed函數來完成。

 let input = 0.004178174922295; const decimalCount = input.toString().length - 2; const unit = Math.pow(10, -1 * decimalCount); console.log(input); const loopInterval = setInterval(() => { input = Number((input - unit).toFixed(decimalCount)); console.log(input); if (input == 0) { clearInterval(loopInterval); } }, 1000);

如果輸入的數字有很多小數,所以它是作為string類型接收的(不能使用number類型表示),則需要使用字符串進行減法,如下所示。

 const input = '0.0041781749222934534534534535'; const inputArr = input.split('.'); const intNum = inputArr[0]; // Present integer let decimals = inputArr[1]; // Present decimals after '.' const unit = 1; function replaceAt(str, index, replace) { return str.substring(0, index) + replace + str.substring(index + 1); } console.log(input); const loopInterval = setInterval(() => { let index = decimals.length - 1; while (parseInt(decimals[index]) < unit) { decimals = replaceAt(decimals, index --, '9'); } decimals = replaceAt(decimals, index, `${parseInt(decimals[index]) - unit}`); console.log(`${intNum}.${decimals}`); }, 1000);

暫無
暫無

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

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