Number.prototype.toExponential()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
toExponential() method ç¨ä¾å°æ¸åè½æãç§å¸è¨æ¸æ³ãæ ¼å¼ã
èªæ³
numObj.toExponential([fractionDigits])
忏
| 忏 | å¯é¸ | é»èªå¼ | é¡å | 說æ |
|---|---|---|---|---|
fractionDigits |
â | é»èªç¡å¯è½å°æ¸å宿´é¡¯ç¤º | Numberï¼æ£æ´æ¸ï¼ |
å°æ¸é»å¾ç使¸ãéçº 0 è³ 20 ä¹éã |
åå³å¼
ä¸ stringï¼å°æ¸å以ãç§å¸è¨æ¸æ³ãæ ¼å¼è¡¨ç¤ºåºä¾
Exceptions
RangeError-
è¥
fractionDigitsè¶ åºç¯åï¼å¯æ¥åçç¯åæ¯ 0 ï½ 20 ä¹éçæ£æ´æ¸ï¼è§¸ç¼RangeErrorã TypeError-
è¥åæ¸
fractionDigits䏿¯Numberï¼å觸ç¼TypeErrorã
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().
ç¯ä¾
>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
è¦ç¯
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-number.prototype.toexponential> |