Number.prototype.toExponential() - JavaScript 编辑
The toExponential()
method returns a string representing the Number
object in exponential notation.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.Syntax
numObj.toExponential([fractionDigits])
Parameters
fractionDigits
Optional- Optional. An integer specifying the number of digits after the decimal point. Defaults to as many digits as necessary to specify the number.
Return value
A string representing the given Number
object in exponential notation with one digit before the decimal point, rounded to fractionDigits
digits after the decimal point.
Exceptions
RangeError
- If
fractionDigits
is too small or too large. Values between0
and20
, inclusive, will not cause aRangeError
. Implementations are allowed to support larger and smaller values as well. TypeError
- If this method is invoked on an object that is not a
Number
.
Description
If the fractionDigits
argument is omitted, the number of digits after the decimal point defaults to the number of digits necessary to represent the value uniquely.
If you use the toExponential()
method for a numeric literal and the numeric literal has no exponent and no decimal point, leave whitespace(s) before the dot that precedes the method call to prevent the dot from being interpreted as a decimal point.
If a number has more digits than requested by the fractionDigits
parameter, the number is rounded to the nearest number represented by fractionDigits
digits. See the discussion of rounding in the description of the toFixed()
method, which also applies to toExponential()
.
Examples
Using 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
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Number.prototype.toExponential' in that specification. |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论