Why toFixed ?
The JavaScritpt toFixed () method is used to format a number using a fixed point representation. If the decimal number we want is higher, blank values can be added to create the desired decimal length.
Syntax:
number.toFixed( value )
- value ( optional ): The number of digits after the decimal point. Default is 0.
JavaScript toFixed Examples
Example 1
In the following example, the num variable is assigned the number 2.4363. Then I set the value as myFix, with the help of the variable toFixed (). Then I return the value.
let num = 2.4363;
let myFix = num.toFixed(2);
console.log(myFix)
output:
2.44
Example 2
let num1 = 4.3621;
let num2 = 9.9851;
let num3 = 54.399999;
let num4 = 1.46e+20;
let num5 = -3.23299;
console.log(num1.toFixed(3));
console.log(num2.toFixed(2));
console.log(num3.toFixed(5));
console.log(num4.toFixed(4));
console.log(num5.toFixed(3));
output:
4.362
9.99
146000000000000000000.0000
-3.233
Browser Support
| Chrome | yes |
| Edge | yes |
| Firefox | 1 |
| Internet Explorer | yes |
| Opera | yes |
| Safari | yes |
| Android webview | yes |
| Chrome for Android | yes |
| Edge mobile | yes |
| Firefox for Android | 4 |
| Opera Android | yes |
Simply desire tо say your aгticle is as amazing. The clearness іn your post is simply greаt and that
i could think you are knowledgeable in this subject. Ꮃell
with your permissіon let me tօ seize yօur feed to stay up to date witһ imminent post.
Thanks a million and please carry on the enjoyable work.
LikeLike