可以到http://www.aiice.com/310.html查看更好效果!

具体方法

1,This conversion-through-concatenation feature of JavaScript results in an idiom that you may occasionally see: to convert a number to a string, simply add the empty string to it:

  1. var n_as_string = n + "";  
var n_as_string = n + "";

2,To make number-to-string conversions more explicit, use the String( ) function:

  1. var string_value = String(number);  
var string_value = String(number);

3,Another technique for converting numbers to strings uses the toString( ) method:

  1. string_value = number.toString( );  
string_value = number.toString( );

toString( )有参数可以转换其它进制的字符串 如:

  1. var n = 17;   
  2. binary_string = n.toString(2);        // Evaluates to "10001"   
  3. octal_string = "0" + n.toString(8);   // Evaluates to "021"   
  4. hex_string = "0x" + n.toString(16);   // Evaluates to "0x11"  
var n = 17;
binary_string = n.toString(2);        // Evaluates to "10001"
octal_string = "0" + n.toString(8);   // Evaluates to "021"
hex_string = "0x" + n.toString(16);   // Evaluates to "0x11"

4,ECMAScript v3 and JavaScript 1.5 solve this problem by adding three new number-to-string methods to the Number class. toFixed( ) converts a number to a string and displays a specified number of digits after the decimal point. It does not use exponential notation. toExponential( ) converts a number to a string using exponential notation, with one digit before the decimal point and a specified number of digits after the decimal point. toPrecision( ) displays a number using the specified number of significant digits. It uses exponential notation if the number of significant digits is not large enough to display the entire integer portion of the number. Note that all three methods round the trailing digits of the resulting string as appropriate(都会四舍五入). Consider the following examples:

  1. var n = 123456.789;   
  2. n.toFixed(0);         // "123457"   
  3. n.toFixed(2);         // "123456.79"   
  4. n.toExponential(1);   // "1.2e+5"   
  5. n.toExponential(3);   // "1.235e+5"   
  6. n.toPrecision(4);     // "1.235e+5"   
  7. n.toPrecision(7);     // "123456.8"  
var n = 123456.789;
n.toFixed(0);         // "123457"
n.toFixed(2);         // "123456.79"
n.toExponential(1);   // "1.2e+5"
n.toExponential(3);   // "1.235e+5"
n.toPrecision(4);     // "1.235e+5"
n.toPrecision(7);     // "123456.8"