Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to force a number to display in exponential notation?
Use the toExponential() method to force a number to display in exponential notation, even if the number is in the range in which JavaScript normally uses standard notation.
Syntax
number.toExponential(fractionDigits)
Parameters
- fractionDigits - An optional integer specifying the number of digits after the decimal point. Defaults to as many digits as necessary to specify the number.
Return Value
Returns a string representing the number in exponential notation.
Example: Basic Usage
You can force a number to display in exponential notation as follows:
<html>
<head>
<title>JavaScript Method toExponential()</title>
</head>
<body>
<script>
var num = 77.1234;
var val = num.toExponential();
document.write("num.toExponential() is : " + val);
document.write("<br />");
val = num.toExponential(4);
document.write("num.toExponential(4) is : " + val);
document.write("<br />");
val = num.toExponential(2);
document.write("num.toExponential(2) is : " + val);
document.write("<br />");
val = 77.1234.toExponential();
document.write("77.1234.toExponential() is : " + val);
</script>
</body>
</html>
num.toExponential() is : 7.71234e+1 num.toExponential(4) is : 7.7123e+1 num.toExponential(2) is : 7.71e+1 77.1234.toExponential() is : 7.71234e+1
Example: Different Number Types
<html>
<head>
<title>toExponential() Examples</title>
</head>
<body>
<script>
// Large number
document.write("123456789..toExponential(): " + 123456789..toExponential());
document.write("<br />");
// Small decimal
document.write("0.000123.toExponential(): " + 0.000123.toExponential());
document.write("<br />");
// With specific precision
document.write("1234.5678.toExponential(3): " + 1234.5678.toExponential(3));
</script>
</body>
</html>
123456789..toExponential(): 1.23456789e+8 0.000123.toExponential(): 1.23e-4 1234.5678.toExponential(3): 1.235e+3
Key Points
- The method always returns a string in exponential notation format
- If no argument is provided, JavaScript uses as many digits as needed
- The fractionDigits parameter controls decimal precision
- Useful for displaying very large or very small numbers in a compact format
Conclusion
The toExponential() method provides a reliable way to format numbers in scientific notation. Use the optional parameter to control decimal precision based on your display requirements.
Advertisements
