簡體   English   中英

Javascript將十進制數字格式轉換為十進制后的樹編號

[英]Javascript format a decimal number to tree numbers after decimal

我該如何格式化數字

0.00012006 to 0.00012
0.00004494 to 0.0000449
0.000000022732 to 0.0000000227 without becoming a number like 2.3e-8 

我想知道如何快速/有效地更改數字。
我想知道如何轉換這些數字,但是如果有人只知道如何格式化它們,我也想知道。

您可以算出數字的位置,然后在其上加上2代表toFixed

 function three(v) { var n = Math.floor(Math.log(v) / Math.LN10); return v.toFixed(n < 2 ? 2 - n : 0); } var n = [0.00012006, 0.00004494, 0.000000022732, 0.100101, 0.1100001, 1.000001, 12000, 10, 1e10]; console.log(n.map(three)); 

像這樣使用yourNumber.toFixed(numberOfDigitsAfterDot)

 function format(n) { var _n = n; // count the position of the first decimal var count = 0; do { n = n * 10; count++; } while(n < 1); return _n.toFixed(count + 2); } var num = 0.000000022732; console.log(format(num)); 

暫無
暫無

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

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