
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Subtraction of Two Float Values in JavaScript
As we are familiar with the subtraction of two float values and we know how to use the subtraction condition between float values. To correctly subtract two float values, use parseFloat() along with toFixed(). Let's dive into the article to learn more about JavaScript subtraction of two float values.
The parseFloat()function
The string is taken as input and converted into a floating-point value using the parseFloat() method. The function returns NaN, or not a number, if the string is empty or if its first character is not a numeric value. Up until the point where it runs into a character that isn't a Number, it actually returns a parsed floating-point number.
Syntax
Following is the syntax for parseFloat() function.
parseFloat(Value)
The toFixed()function
In JavaScript, a number can be formatted using fixed-point notation by using the toFixed() method. It can be applied to numbers to format them with a certain number of digits to the right of the decimal.
Syntax
Following is the syntax for toFixed()
number.toFixed( value )
let's look into the following examples to understand more about JavaScript subtraction of two float values.
Example
In the following example, we are running the script to subtract two float numbers.
<!DOCTYPE html> <html> <body> <h3> Click the below button to subtract two float values</h3> <button onclick="flchk()">Click me</button> <p id = "value1"></p> <p id = "value2"></p> <p id = "value3"></p> <script> function flchk() { var timin=parseFloat(12.10) document.getElementById("value1").innerHTML = "First float Value: " + timin; var timout=parseFloat(14.15) document.getElementById("value2").innerHTML ="Second float Value: " + timout; var Tottim=(timout-timin) document.getElementById("value3").innerHTML = "After subtrtacting:" + Tottim } </script> </body> </html>
When the script gets executed, it will generate an output consisting of a button and text. When the user clicks the button, the event gets triggered and displays two float values along with their result after subtraction on the webpage.
Example
Considering the another example where we are using the parsefloat() and tofixed() to subtract two float values.
<!DOCTYPE html> <html> <body> <script> total = 2.2; discount_amt = 2.3; document.write(parseFloat(total).toFixed(1) + ' ' + "<br>" + parseFloat(discount_amt).toFixed(1) + "<br>"); total = parseFloat(total).toFixed(1) - parseFloat(discount_amt).toFixed(1); document.write(parseFloat(total).toFixed(1)); </script> </body> </html>
On running the above script, the web-browser displays the two float values on the webpage along with a result value obtained by subtracting the two float values that occurred as a result of an event that was triggered when the user ran the script.
Example
Let's look into the another example, where we are running script to subtract two float values.
<!DOCTYPE html> <html> <body> <script> var firstValue=4.3; var secondValue=3.8; document.write("The first Value="+parseFloat(firstValue).toFixed(1)+ "<br>" +" The second Value="+parseFloat(secondValue).toFixed(1) + "<br>") var result = parseFloat(firstValue).toFixed(1) -parseFloat(secondValue).toFixed(1); document.write("Result is="+result); </script> </body> </html>
When the script gets executed, it will generate an output consisting of two float values along with a result value obtained by the event that was triggered when the script got executed, making the two float values subtract.