[英]How to prevent very small numbers from being converted to scientific notation?
[英]scientific notation with small numbers
我需要處理以科學記數法書寫的數字。 我知道目前 JS 僅對大小數字使用科學記數法。
例子:
1000000000000000000000 === 1e+21
0.0000001 === 1e-7
我希望每個數字都使用相同的符號。
例子:
10
應該是1e+1
我已經嘗試使用.toExponential
方法,但數字被轉換為string
。
我需要保留number
類型。
有什么建議嗎?
您可以使用Number
將科學記數法字符串轉換回數字。 .toExponential
將在規范中返回一個字符串,所以這就是你得到的。
console.log( Number('1e+4') ); console.log( Number('1e-4') ); console.log( typeof Number('1e-4') );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.