2026年9月21日 星期一

還原 Number.toExponential() 的結果

 還原 Number.toExponential() 的結果

前言

  Number.toExponential() 的輸出結果是個字串,如果要把還原回原本的數值,之前是靠解析該字串的內容套用算式來還原,但其實有更簡便的方法,在此把學習的過程做個紀錄。


內容

  範例如下

let num = 1234567;
console.log( num.toExponential(3) );//1.235e+6
console.log( 1.235 * ( 10 ** 6 ) );//1235000
console.log( Number.parseFloat( num.toExponential(3) ) );//1235000


範例展示要被還原的字串 1.235e+6 ,並附上了之前的還原算式,最後就是這次的簡便方法,就是透過 Number.parseFloat() 就可以還原出跟之前算式一樣的結果。


參考資料

[ developer.mozilla.org ] Number.prototype.toExponential()


相關文章與資料

關於 Number.toExponential()

控制 Number.toExponential() 輸出的精度

沒有留言:

張貼留言