首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

number.toExponential

toExponential()方法以指数表示法返回该数值字符串表示形式。

语法

代码语言:javascript
复制
numObj.toExponential([fractionDigits])

参数

fractionDigits可选。一个整数,用来指定小数点后有几位数字。默认情况下用尽可能多的位数来显示数字。

返回值

一个用幂的形式 (科学记数法) 来表示Number对象的字符串。小数点后以fractionDigits 提供的值来四舍五入。如果?fractionDigits参数被忽略了,小数点后的将尽可能用最多的位数来表示该数值。

异常

RangeError如果fractionDigits太小或太大将会抛出该错误。介于 0 和 20(包括20)之间的值不会引起?RangeError?。 执行环境也可以支持更大或更小范围。TypeError如果该方法在一个非数值类型对象上调用。

描述

如果fractionDigits省略参数,则小数点后面的位数默认为唯一表示值所需的位数。

如果对数字文字使用toExponential()方法,并且数字文字没有指数以及小数点,请在方法调用之前的点前面留出空格,以防止点被解释为小数点。

如果一个数字的位数多于fractionDigits参数所要求的位数,则该数字四舍五入到最接近的数字位数。在这个讨论中的toFixed()方法也适用于toExponential()

示例

使用toExponential

代码语言:javascript
复制
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)

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com