js中的Number方法


1.Number.toExponential(fractionDigits)

把number轉換成一個指數形式的字符串。可選參數控制其小數點后的數字位數。它必須在0~20之間。

例如:

 1 document.writeln(Math.PI.toExponential(0));
 2 document.writeln(Math.PI.toExponential(2));
 3 document.writeln(Math.PI.toExponential(7));
 4 document.writeln(Math.PI.toExponential(16));
 5 document.writeln(Math.PI.toExponential(  ));
 6 
 7 // Produces
 8 
 9 3e+0
10 3.14e+0
11 3.1415927e+0
12 3.1415926535897930e+0
13 3.141592653589793e+0

 

 

2.number.toFixed(fractionDigits)

把number數轉換成一個十進制數形式的字符串。可選參數控制其小數點后的數字位數。它的值必須在0~20之間,默認為0,例如:

 1 document.writeln(Math.PI.toFixed(0));
 2 document.writeln(Math.PI.toFixed(2));
 3 document.writeln(Math.PI.toFixed(7));
 4 document.writeln(Math.PI.toFixed(16));
 5 document.writeln(Math.PI.toFixed(  ));
 6 
 7 // Produces
 8 
 9 3
10 3.14
11 3.1415927
12 3.1415926535897930
13 3

 

 

3.number.toPrecision(precision)

把這個number轉化為一個十進制形式的字符串。可選參數控制字符精度,它的精度必須在0~21之間。例如:

 1 document.writeln(Math.PI.toPrecision(2));
 2 document.writeln(Math.PI.toPrecision(7));
 3 document.writeln(Math.PI.toPrecision(16));
 4 document.writeln(Math.PI.toPrecision(  ));
 5 
 6 // Produces
 7 
 8 3.1
 9 3.141593
10 3.141592653589793
11 3.141592653589793

 

 

4.number.toString(radix)

將number轉換成一個字符串。可選參數控制基數,它的值必須在2~36之間。默認radix的值是10。radix參數最常用的是整數,但是它可以用任意數字。

在最普通的情況下,該方法可以寫成String(number)

例如:

 1 document.writeln(Math.PI.toString(2));
 2 document.writeln(Math.PI.toString(8));
 3 document.writeln(Math.PI.toString(16));
 4 document.writeln(Math.PI.toString(  ));
 5 
 6 // Produces
 7 
 8 11.001001000011111101101010100010001000010110100011
 9 3.1103755242102643
10 3.243f6a8885a3
11 3.141592653589793

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM