
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript subtraction of two float values?
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.
- Related Articles
- Calculate Subtraction of diagonals-summations in a two-dimensional matrix using JavaScript
- Performing the subtraction operation without the subtraction operator in JavaScript
- How to parse float with two decimal places in JavaScript?
- What is Subtraction Operator (-) in JavaScript?
- Non-negative set subtraction in JavaScript
- PHP program to compare float values
- Comparing corresponding values of two arrays in JavaScript
- Calculate the absolute value of float values in Numpy
- PHP program to calculate the repeated subtraction of two numbers
- 8086 program to determine subtraction of corresponding elements of two arrays
- Convert a list of string coords into two float lists of Lat/Longitude coordinates in JavaScript?
- Modulus of two float or double numbers using C
- Split float value in two columns of a MySQL table?
- Merge two arrays with alternating Values in JavaScript
- Group values in array by two properties JavaScript
