How to add float numbers using JavaScript ? - GeeksforGeeks

11 Dec, 2019
Given two or more numbers and 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:
Approach 1:
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.
<!DOCTYPE HTML> <html>   <head>     <title>         Float sum with JavaScript.    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP" style =        "font-size: 15px; font-weight: bold;">    </p>          <button onclick = "gfg_Run()">         Click Here    </button>          <p id = "GFG_DOWN" style = "color:green;        font-size: 20px; font-weight: bold;">    </p>          <script>        var el_up = document.getElementById("GFG_UP");        var el_down = document.getElementById("GFG_DOWN");                  el_up.innerHTML = "Click on the button to get the"                          + " sum in desired format.";                  var val = parseFloat('2.3')+parseFloat('2.4');        el_down.innerHTML = "2.3 + 2.4 = " + val;                  function gfg_Run() {            el_down.innerHTML = "2.3 + 2.4 = "                         + (parseFloat('2.3') +                         parseFloat('2.4')).toFixed(2);        }     </script> </body>   </html>
Output:
Before clicking on the button:
After clicking on the button:
Approach 2:
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.
<!DOCTYPE HTML> <html>   <head>     <title>         Float sum with javascript.    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP" style =        "font-size: 15px; font-weight: bold;">    </p>          <button onclick = "gfg_Run()">         Click Here    </button>          <p id = "GFG_DOWN" style = "color:green;        font-size: 20px; font-weight: bold;">    </p>          <script>        var el_up = document.getElementById("GFG_UP");        var el_down = document.getElementById("GFG_DOWN");                  el_up.innerHTML = "Click on the button to get the"                          + " sum in desired format.";                  var val = parseFloat('2.3')+parseFloat('2.4');        el_down.innerHTML = "2.3 + 2.4 = " + val;                  function gfg_Run() {            el_down.innerHTML = "2.3 + 2.4 = " +                     Math.round((parseFloat('2.3')                    + parseFloat('2.4'))*100)/100;        }     </script> </body>   </html>
Output:
Before clicking on the button:
After clicking on the button:
javascript-math
JavaScript-Misc
JavaScript-Numbers
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?