📜  打字稿 | toExponential() 方法

📅  最后修改于: 2022-05-13 01:56:38.141000             🧑  作者: Mango

打字稿 | toExponential() 方法

TypeScript 中的toExponential() 方法用于将数字转换为其指数形式。此方法根据给定参数以指数表示法返回表示给定数字对象的字符串。

句法:

number.toExponential( [fractionDigits] )

参数:此方法接受如上所述和如下所述的单个参数:

  • fractionDigits:它是一个整数值,指定小数点后的位数。它是一个可选参数。

返回值: TypeScript 中的toExponential()方法返回一个字符串,该字符串以指数表示法表示数字对象,小数点前有一位。

下面的示例说明了 TypeScript 中toExponential()方法的工作原理:

示例 1:

TypeScript
// Define a number to find the
// exponential notation
var num1 = 2762.30
  
// Find the exponential value
var val = num1.toExponential();
  
// Display the value
console.log(val)


TypeScript
// Define a number to find the
// exponential notation
var num2 = 23456
  
// Find the exponential value
// with two digits after the
// decimal point
var val = num2.toExponential(2);
  
// Display the value
console.log(val)


输出:

2.7623e+3

示例 2:

打字稿

// Define a number to find the
// exponential notation
var num2 = 23456
  
// Find the exponential value
// with two digits after the
// decimal point
var val = num2.toExponential(2);
  
// Display the value
console.log(val)

输出:

2.34e+4