How to Format a Float in JavaScript?
Last Updated :
10 Oct, 2024
Formatting a float number means rounding off a number up to a given number of decimal places.
Below are the approaches to format a float in JavaScript:
Using Math.ceil() method
The JavaScript Math.ceil method is used to return the smallest integer greater than or equal to a given number.
Example: This example shows these of the above-explained approach.
JavaScript
// Define the float value
let n = 6.56759;
let ceilValue = Math.ceil(n);
console.log("Math.ceil(n) =", ceilValue);
Using toFixed() method
The toFixed() method rounds the string to a specified number of decimals.
Example: This example shows these of the above-explained approach.
JavaScript
// Define the float value
let n = 6.56759;
let roundValue = Math.round(n);
console.log("Math.round(n) =", roundValue);
Using Math.round() method
The JavaScript Math.round() method rounds a number to the nearest integer.
Example: This example shows these of the above-explained approach.
JavaScript
// Define the float value
let n = 6.56759;
// Adjust the number inside toFixed() to
// change decimal places
let fixedValue = n.toFixed(2);
console.log("n.toFixed(2) =", fixedValue);
Outputn.toFixed(2) = 6.57
Using Math.floor() Method
The Math.floor() function is used to round off the number passed as a parameter to its nearest integer in
Example: This example shows these of the above-explained approach.
JavaScript
// Define the float value
let n = 6.56759;
// Rounds off to the floor value
let floorValue = Math.floor(n);
console.log("Math.floor(n) =", floorValue);
// Rounds off up to one decimal place
let floorOneDecimal = Math.floor(n * 10) / 10;
console.log("Math.floor(n * 10) / 10 =", floorOneDecimal);
// Rounds off up to two decimal places
let floorTwoDecimals = Math.floor(n * 100) / 100;
console.log("Math.floor(n * 100) / 100 =", floorTwoDecimals)
OutputMath.floor(n) = 6
Math.floor(n * 10) / 10 = 6.5
Math.floor(n * 100) / 100 = 6.56
Using float.toExponential() method
The toExponential() method is used to convert a number to its exponential form. It returns a string representing the Number object in exponential notation.
Example: This example shows these of the above-explained approach.
JavaScript
// Define float values
let n1 = 5.569999999999999999999;
let n2 = 5.569999999999;
// The complexity of the float results
// in its conversion
console.log("n1.toExponential() =", n1.toExponential());
console.log("n2.toExponential() =", n2.toExponential());
Outputn1.toExponential() = 5.57e+0
n2.toExponential() = 5.569999999999e+0
Using number.toPrecision() Method
The toPrecision() method is used to format a number to a specific precision or length. If the formatted number requires more number of digits than the original number then decimals and nulls are also added to create the specified length.
Example: This example shows these of the above-explained approach.
JavaScript
// Define float values
let n1 = 13.3714;
let n2 = 0.0016588874;
let n3 = 13.3714;
// Example using toPrecision() on n1
console.log("n1.toPrecision() =", n1.toPrecision());
console.log("n1.toPrecision(2) =", n1.toPrecision(2));
// Example using toPrecision() on n2
console.log("n2.toPrecision() =", n2.toPrecision());
console.log("n2.toPrecision(2) =", n2.toPrecision(2));
// Example using toPrecision() on n3
console.log("n3.toPrecision() =", n3.toPrecision());
console.log("n3.toPrecision(2) =", n3.toPrecision(2));
Outputn1.toPrecision() = 13.3714
n1.toPrecision(2) = 13
n2.toPrecision() = 0.0016588874
n2.toPrecision(2) = 0.0017
n3.toPrecision() = 13.3714
n3.toPrecision(2) = 13