How to format a number to a fixed number of decimal places with vanilla JS

Let’s imagine you have a number, pi, with five decimal places.
let pi = 3.14159; You want to display with only two. How would you do that?
The Number.toFixed() method formats a number to a fixed number of decimal places. Call it on the number, and pass in the number of decimal places as an argument.
It returns a formatted string.
let pi = 3.14159; // returns “3.14” pi.toFixed(2); If you call the Number.


This content originally appeared on Go Make Things and was authored by Go Make Things

Let’s imagine you have a number, pi, with five decimal places.

let pi = 3.14159;

You want to display with only two. How would you do that?

The Number.toFixed() method formats a number to a fixed number of decimal places. Call it on the number, and pass in the number of decimal places as an argument.

It returns a formatted string.

let pi = 3.14159;

// returns "3.14"
pi.toFixed(2);

If you call the Number.toFixed() method on a number that has fewer decimal places than the number specified, 0’s will be added.

let eleven = 11;

// returns "11.000"
eleven.toFixed(3);

Here’s a demo.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2021-06-09T14:30:00+00:00) How to format a number to a fixed number of decimal places with vanilla JS. Retrieved from https://www.scien.cx/2021/06/09/how-to-format-a-number-to-a-fixed-number-of-decimal-places-with-vanilla-js/

MLA
" » How to format a number to a fixed number of decimal places with vanilla JS." Go Make Things | Sciencx - Wednesday June 9, 2021, https://www.scien.cx/2021/06/09/how-to-format-a-number-to-a-fixed-number-of-decimal-places-with-vanilla-js/
HARVARD
Go Make Things | Sciencx Wednesday June 9, 2021 » How to format a number to a fixed number of decimal places with vanilla JS., viewed ,<https://www.scien.cx/2021/06/09/how-to-format-a-number-to-a-fixed-number-of-decimal-places-with-vanilla-js/>
VANCOUVER
Go Make Things | Sciencx - » How to format a number to a fixed number of decimal places with vanilla JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/09/how-to-format-a-number-to-a-fixed-number-of-decimal-places-with-vanilla-js/
CHICAGO
" » How to format a number to a fixed number of decimal places with vanilla JS." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2021/06/09/how-to-format-a-number-to-a-fixed-number-of-decimal-places-with-vanilla-js/
IEEE
" » How to format a number to a fixed number of decimal places with vanilla JS." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/06/09/how-to-format-a-number-to-a-fixed-number-of-decimal-places-with-vanilla-js/. [Accessed: ]
rf:citation
» How to format a number to a fixed number of decimal places with vanilla JS | Go Make Things | Sciencx | https://www.scien.cx/2021/06/09/how-to-format-a-number-to-a-fixed-number-of-decimal-places-with-vanilla-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.