JavaScript is_float
Returns true if variable is float point
1 2 3 4 56 7 8 9 1011 12 13 14 15 | function is_float (mixed_var) { // Returns true if variable is float point // // version: 1109.2015 // discuss at: http://phpjs.org/functions/is_float // + original by: Paulo Freitas // + bugfixed by: Brett Zamir (http://brett-zamir.me) // + improved by: WebDevHobo (http://webdevhobo.blogspot.com/) // + improved by: Rafał Kukawski (http://blog.kukawski.pl) // % note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes // % note 1: it different from the PHP implementation. We can't fix this unfortunately. // * example 1: is_float(186.31); // * returns 1: true return +mixed_var === mixed_var && !!(mixed_var % 1); } |
Examples
Running
1 | is_float(186.31); |
Should return
1 | true |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have is_float goodness in JavaScript.

version in one line:
CoursesWeb
Apr 30th
For is_float i use this version:
function is_float(n) { return n===+n && n!==(n|0); }- is_float() doesn't always work if you are validating form input. This is because form inputs are strings, even if the user typed a number.