[英]Javascript: prevent parseFloat from converting large number to scientific notation
[英]javascript converting scientific notation, how to determine the number after power of 10
如果我在瀏覽器控制台中輸入一個數字,結果是:
1000000000000000000000 --> 1e+21
0.00000000003453 --> 3.453e-11
我想確定e
之后的數字。 在此示例中,數字為21
或-11
謝謝!
您可以使用.toExponential()
將數字轉換為字符串,然后基於e
拆分並獲取最后一個元素(這將是您的數字),如下所示:
const getExp = n => +(n.toExponential().split('e').pop()) console.log(getExp(1000000000000000000000)) // 21 console.log(getExp(0.00000000003453)); // -11 console.log(getExp(0.000324)); // -4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.