EDDYMENS

Last updated 2024-02-17 11:27:56

Round Up A Number To X Decimal Places | Javascript


Lisez-le en Français

You can use the Number prototype .toFixed [β†’] method in Javascript to Round up a Number to 2 decimal places.

toFixedExample.js

01: let num = 10.15645; 02: let fixedNum = num.toFixed(2); // Rounds to 2 decimal places 03: console.log(fixedNum); // Output: "10.16"

Do note that the result will be converted to a string so if you want to further perform some mathematical computation on the result you will have to covert this back to a float using the parseFloat [β†’] function.

parseFloatExample.js

01: let num = 10.15645; 02: let fixedNum = num.toFixed(2); // "10.16" 03: let floatValue = parseFloat(fixedNum); // 10.16 04: console.log(floatValue); // Output: 10.16

Here is another article you might like 😊 "Diary Of Insights: A Documentation Of My Discoveries"