How to add float numbers using JavaScript?


In this article, you will understand how to add float numbers using JavaScript. Float values in JavaScript are defined as parseFloat(string).

Example 1

In this example, let’s understand adding float values without using functions.

let inputFloat1 = parseFloat(2.3)
let inputFloat2 = parseFloat(3.5)

console.log("The two float values are defined as ", inputFloat1 ,"and", inputFloat2)

let result = inputFloat1 + inputFloat2
console.log("
The sum of the float values is: ",result)

Explanation

  • Step 1 − Define two float values inputFloat1 and inputFloat2.

  • Step 2 − Add the two float values using addition operator (+).

  • Step 3 − Display the result.

Example 2

In this example, let’s understand adding float values without using functions.

function addFloat(inputFloat1, inputFloat2){
   let result = inputFloat1 + inputFloat2
   console.log("
The sum of the float values is: ",result) } let inputFloat1 = parseFloat(2.3) let inputFloat2 = parseFloat(3.5) console.log("The two float values are defined as ", inputFloat1 ,"and", inputFloat2) addFloat(inputFloat1, inputFloat2)

Explanation

  • Step 1 − Define two float values inputFloat1 and inputFloat2.

  • Step 2 − Define a function addFloat that takes two float values as parameters.

  • Step 3 − In the function, add the two float values using the addition operator (+).

  • Step 4 − Display the result.

Updated on: 16-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements