Table of Content

## What is a floating point number in programming?

The expression floating stage comes from the very fact that there’s not any fixed number of digits before and after the decimal point; this is, the decimal point can float. Additionally, there are representations where the number of digits before and after the decimal point set, known as fixed-point representations.

Generally, floating-point representations are slower and less precise than fixed-point representations, but they can deal with a more extensive variety of numbers.

Be aware that many floating-point amounts a computer may represent are only approximations. Among those challenges in programming using floating-point worth is making sure the approximations lead to consistent results. In case the developer isn’t cautious, small discrepancies from the approximations can muster into the point at which the final results become meaningless.

Since math with floating-point numbers needs a fantastic deal of computing power, lots of microprocessors include a processor, referred to as a floating point unit (FPU ), technical for acting floating-point arithmetic.

Example of some floating numbers:

* 7.2935

* -12.72

* 1/2

* 12.4e3 [ Equivalent to 12.4 x 103 ]

* 4E-3 [ Equivalent to 4 x 10-3 ]

## How to validate floating numbers?

At times it’s required to enter a number with decimal part especially for amount, duration, height, numbers, etc. We can utilize a floating point number in a variety of ways, like the one below:

Javascript function to check whether a field input contains a number with no exponent, mandatory integer and fraction and optional sign.

<form name="form1" action="#"> <ul> <li><input type='text' name='text1'/></li><li> </li> <li><input type="submit" name="submit" value="Submit" onclick="CheckDecimal(document.form1.text1)"/></li> <li> </li> </ul> </form>

function CheckDecimal(inputtxt) { var decimal= /^[-+]?[0-9]+\.[0-9]+$/; if(inputtxt.value.match(decimal)) { alert('Correct, try another...') return true; } else { alert('Wrong...!') return false; } }

The code always depends on your validation needs. This small example can serve as a start to use floating numbers in JavaScript in the right way.