This JavaScript tutorial explains how to use the Number technique called toFixed() with syntax and examples.
Description
In JavaScript, toFixed() is a Number method that is used to convert a quantity to fixed-point notation (rounding the result the place necessary) and return its price as a string. Because toFixed() is a method of the Number object, it must be invoked via a precise instance of the Number class.
Syntax
In JavaScript, the syntax for the toFixed() method is:
number.toFixed([decimalPlaces]);
Parameters or Arguments
decimalPlaces Optional. It is the variety of digits after the decimal area to display in the result. If this parameter is omitted, the decimalPlaces will default to zero
Returns
The toFixed() approach converts a range to fixed-point notation with the indicated number of decimalPlaces (rounding the end result where necessary) and then returns its cost as a string.
If greater decimal places are required than was once present in the unique number, the toFixed() method will pad the end result with 0’s after the decimal place.
Note
The toFixed() technique will round the resulting price if necessary. The toFixed() method will pad the resulting fee with 0’s if there are now not ample decimal places in the original number. The toFixed() technique does not alternate the value of the unique number.
Example
Let’s take a seem at an instance of how to use the toFixed() approach in JavaScript.
For example:
var totn_number = 123.456789;
console.log(totn_number.toFixed());
console.log(totn_number.toFixed(1));
console.log(totn_number.toFixed(2));
In this example, we have invoked the toFixed() approach using the Number class.
We have written the output of the toFixed() technique to the net browser console log, for demonstration purposes, to show what the toFixed() approach returns.
The following will be output to the web browser console log:
123
123.5
123.46
In this example, the first output to the console log back the string fee “123” which is the fixed-point notation for 123.456789 rounded to 0 decimal places.
The 2nd output to the console log again the string fee “123.5” which is the fixed-point notation for 123.456789 rounded to 1 decimal place.
The third output to the console log returned the string cost “123.46” which is the fixed-point notation for 123.456789 rounded to two decimal places.
Specifying an Exponential Notation
The toFixed() method can additionally manage converting exponential notation values to fixed-point notation.
So if we rewrote our numeric value of 123.456789 as the equal exponential fee (123.456789 = 1.23456789e+2), we could exchange our instance as follows:
var totn_number = 1.23456789e+2;
console.log(totn_number.toFixed());
console.log(totn_number.toFixed(1));
console.log(totn_number.toFixed(2));
The following will be output to the net browser console log:
123
123.5
123.46
In this example, the first output to the console log returned the string fee “123” which is the fixed-point notation for 1.23456789e+2 rounded to zero decimal places.
The second output to the console log back the string value “123.5” which is the fixed-point notation for 1.23456789e+2 rounded to 1 decimal place.
The 0.33 output to the console log lower back the string fee “123.46” which is the fixed-point notation for 1.23456789e+2 rounded to two decimal places.
Padding the Decimal Places
Finally, let’s explore how the toFixed() technique pads the end result with 0’s if there are no longer ample decimal places in the authentic number.
For example:
var totn_number = 123.45;
console.log(totn_number.toFixed(3));
console.log(totn_number.toFixed(4));
The following will be output to the web browser console log:
123.450
123.4500
In this example, the first output to the console log back the string price “123.450” which is the fixed-point notation for 123.45 padded to 3 decimal locations with 0’s.
The 2d output to the console log back the string cost “123.4500” which is the fixed-point notation for 123.45 padded to four decimal places with 0’s.
Leave a Review