How to add float numbers using JavaScript ? Last Updated : 31 May, 2024 Comments Improve Suggest changes Like Article Like Report Given two or more numbers the task is to get the float addition in the desired format with the help of JavaScript. There are two methods to solve this problem which are discussed below: Table of Content Using parseFloat() and toFixed() method Using parseFloat() and Math.round() method Using Number() and Intl.NumberFormat method Using parseFloat() and toFixed() method Given two or more numbers, to sum up the float numbers.Use parseFloat() and toFixed() method to format the output accordingly.Example: This example implements the above approach. JavaScript let val = parseFloat('2.3') + parseFloat('2.4'); console.log("2.3 + 2.4 = " + val); function gfg_Run() { console.log("2.3 + 2.4 = " + (parseFloat('2.3') + parseFloat('2.4')).toFixed(2)); } gfg_Run() Output2.3 + 2.4 = 4.699999999999999 2.3 + 2.4 = 4.70 Using parseFloat() and Math.round() method Given two or more numbers, to sum up the float numbers.Use parseFloat() and Math.round() method to get the desired output.Example: This example implements the above approach. JavaScript let val = parseFloat('2.3') + parseFloat('2.4'); console.log("2.3 + 2.4 = " + val); function gfg_Run() { console.log("2.3 + 2.4 = " + Math.round((parseFloat('2.3') + parseFloat('2.4')) * 100) / 100); } gfg_Run() Output2.3 + 2.4 = 4.699999999999999 2.3 + 2.4 = 4.7 Using Number() and Intl.NumberFormat methodGiven two or more numbers, to sum up the float numbers: Use Number() to convert strings to floating-point numbers.Use Intl.NumberFormat to format the output accordingly.Example: JavaScript let val = Number('2.3') + Number('2.4'); console.log("2.3 + 2.4 = " + val); function gfg_Run() { let sum = Number('2.3') + Number('2.4'); let formattedSum = new Intl.NumberFormat('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(sum); console.log("2.3 + 2.4 = " + formattedSum); } gfg_Run(); Output2.3 + 2.4 = 4.699999999999999 2.3 + 2.4 = 4.70 Comment More infoAdvertise with us Next Article How to add float numbers using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-math JavaScript-Numbers JavaScript-Questions +1 More Similar Reads JavaScript Program to Print Multiplication Table of a Number We are given a number n as input, we need to print its table. Below are the different approaches to print multiplication table in JavaScriptJavaScript Program to print multiplication table of a number1. Using a for LoopThis is the most common way to print the multiplication table. A for loop repeats 3 min read What is the use of Math object in JavaScript ? The Math object in JavaScript provides a set of methods and properties for mathematical constants and functions. It is a built-in object that allows for performing mathematical operations and accessing mathematical constants without creating an instance.What is the Math Object?The Math object is an 4 min read How to get median of an array of numbers in JavaScript ? In this article, we will see how to find the median of an array using JavaScript. A median is a middle value in a given set of numbers or data. Examples: Input arr1 : [1 4 7 9]Output : 5.5Explanation : The median of the two sorted array is the middle elements which is 5 if the arrayLength =arr1.leng 3 min read How to calculate greatest common divisor of two or more numbers/arrays in JavaScript ? In this article, we are given two or more numbers/array of numbers and the task is to find the GCD of the given numbers/array elements in JavaScript. Examples: Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {2, 4, 6, 8} Output : 2 The GCD of three or more numbers equals the product of the prim 2 min read How to test a value x against predicate function and returns fn(x) or x in JavaScript ? In this article, we are given a value x, and the task is to check the value against the predicate function. If the value satisfies the condition or predicate return fn(x) else return x. Predicate functions are the function that takes one item as input and returns true or false based on the whether i 3 min read How to Flatten a Given Array up to Specified Depth in JavaScript? Flatten an array in JavaScript is a common task when dealing with nested arrays. The concept of "flatten" means to convert a multi-dimensional array into a single-dimensional array. When you need to control the depth of flattening, JavaScript provides methods to handle this efficiently. This process 2 min read How to get the standard deviation of an array of numbers using JavaScript ? Given an array and the task is to calculate the standard deviation using JavaScript. Example: Input: [1, 2, 3, 4, 5]Output: 1.4142135623730951Input: [23, 4, 6, 457, 65, 7, 45, 8]Output: 145.13565852332775Please refer to Mean, Variance, and Standard Deviation for details. Mean is average of element. 3 min read How to evaluate binomial coefficient of two integers n and k in JavaScript ? The following are the common definitions of Binomial Coefficients. A binomial coefficient C(n, k) can be defined as the coefficient of x^k in the expansion of (1 + x)^n. Binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects 2 min read How to check two numbers are approximately equal in JavaScript ? In this article, we are given two numbers and the task is to check whether the given numbers are approximately equal to each other or not. If both numbers are approximately the same then print true otherwise print false. Example: Input: num1 = 10.3 num2 = 10 Output: true Approach: To check whether t 2 min read How to create slider to map a range of values in JavaScript ? The task is to map a range of values to another range of values like map (0-100) to (100-10000000) in JavaScript. There are two approaches that are discussed below.Approach 1: Use the logarithmic scale to map the range. In this example, first, the log value of the range is calculated and then the sc 2 min read Like