Given two or more floating-point numbers, the task is to calculate their sum and display the result in the desired format using JavaScript.
JavaScript uses the IEEE 754 floating-point format, so some decimal calculations may produce results such as 4.699999999999999 instead of 4.7. Formatting methods can be used when a specific number of decimal places is required.
There are the following approaches to add and format float numbers in JavaScript:
Approach 1: Using parseFloat() and toFixed()
The parseFloat() method converts a string into a floating-point number, while the toFixed() method formats the result to a specified number of decimal places.
- Convert the input values using parseFloat().
- Add the floating-point numbers.
- Use toFixed() to format the result.
- toFixed(2) displays exactly two digits after the decimal point.
const num1 = parseFloat("2.3");
const num2 = parseFloat("2.4");
const sum = num1 + num2;
console.log("Without formatting:", sum);
console.log("With formatting:", sum.toFixed(2));
Output
Without formatting: 4.699999999999999 With formatting: 4.70
Syntax:
const sum = parseFloat(value1) + parseFloat(value2);
const result = sum.toFixed(2);
Note: toFixed() returns a string, not a number.
Approach 2: Using parseFloat() and Math.round()
The Math.round() method can be used to round the result to a specified number of decimal places.
To round a number to two decimal places:
- Multiply the result by 100.
- Apply Math.round().
- Divide the result by 100.
const num1 = parseFloat("2.3");
const num2 = parseFloat("2.4");
const sum = num1 + num2;
console.log("Without formatting:", sum);
const roundedSum = Math.round(sum * 100) / 100;
console.log("With rounding:", roundedSum);
Output
Without formatting: 4.699999999999999 With rounding: 4.7
Syntax:
const sum = parseFloat(value1) + parseFloat(value2);
const result = Math.round(sum * 100) / 100;
Note: Unlike toFixed(), Math.round() returns a number and does not preserve trailing zeros. For example, 4.70 becomes 4.7.
Approach 3: Using Number() and Intl.NumberFormat
The Number() function can convert numeric strings into numbers, while Intl.NumberFormat can format the result according to the required number of decimal places.
- Convert the input values using Number().
- Add the numbers.
- Use Intl.NumberFormat to format the result.
- Set minimumFractionDigits and maximumFractionDigits to control decimal places.
const num1 = Number("2.3");
const num2 = Number("2.4");
const sum = num1 + num2;
console.log("Without formatting:", sum);
const formattedSum = new Intl.NumberFormat("en-US", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).format(sum);
console.log("With formatting:", formattedSum);
Output
Without formatting: 4.699999999999999 With formatting: 4.70
Syntax:
const sum = Number(value1) + Number(value2);
const result = new Intl.NumberFormat("en-US", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).format(sum);
Note: Intl.NumberFormat is useful when the result needs to be displayed in a specific number format, such as currency, percentages, or locale-specific formatting.