JavaScript参考手册
Number
number.toExponential
toExponential()
方法以指数表示法返回该数值字符串表示形式。
语法
numObj.toExponential([fractionDigits])
参数
fractionDigits
可选。一个整数,用来指定小数点后有几位数字。默认情况下用尽可能多的位数来显示数字。
返回值
一个用幂的形式 (科学记数法) 来表示Number
对象的字符串。小数点后以fractionDigits 提供的值来四舍五入。如果 fractionDigits
参数被忽略了,小数点后的将尽可能用最多的位数来表示该数值。
异常
RangeError
如果fractionDigits太小或太大将会抛出该错误。介于 0 和 20(包括20)之间的值不会引起 RangeError
。 执行环境也可以支持更大或更小范围。TypeError
如果该方法在一个非数值类型对象上调用。
描述
如果fractionDigits
省略参数,则小数点后面的位数默认为唯一表示值所需的位数。
如果对数字文字使用toExponential()
方法,并且数字文字没有指数以及小数点,请在方法调用之前的点前面留出空格,以防止点被解释为小数点。
如果一个数字的位数多于fractionDigits
参数所要求的位数,则该数字四舍五入到最接近的数字位数。在这个讨论中的toFixed()
方法也适用于toExponential()
。
示例
使用toExponential
var numObj = 77.1234;
console.log(numObj.toExponential()); // logs 7.71234e+1
console.log(numObj.toExponential(4)); // logs 7.7123e+1
console.log(numObj.toExponential(2)); // logs 7.71e+1
console.log(77.1234.toExponential()); // logs 7.71234e+1
console.log(77 .toExponential()); // logs 7.7e+1
规范
Specification |
Status |
Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) |
Standard |
Initial definition. Implemented in JavaScript 1.5. |
ECMAScript 5.1 (ECMA-262)The definition of 'Number.prototype.toExponential' in that specification. |
Standard |
|
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Number.prototype.toExponential' in that specification. |
Standard |
|
ECMAScript Latest Draft (ECMA-262)The definition of 'Number.prototype.toExponential' in that specification. |
Draft |
|
浏览器兼容性
Feature |
Chrome |
Firefox |
Edge |
Internet Explorer |
Opera |
Safari |
---|---|---|---|---|---|---|
Basic Support |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
Feature |
Android |
Chrome for Android |
Edge mobile |
Firefox for Android |
IE mobile |
Opera Android |
iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
Number相关

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络