---
layout: page
title: "JavaScript floatval function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/floatval:402
- /functions/view/floatval
- /functions/view/402
- /functions/floatval:402
- /functions/402
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's floatval

{% codeblock var/floatval.js lang:js https://raw.github.com/kvz/phpjs/master/functions/var/floatval.js raw on github %}
function floatval (mixed_var) {
  // +   original by: Michael White (http://getsprink.com)
  // %        note 1: The native parseFloat() method of JavaScript returns NaN when it encounters a string before an int or float value.
  // *     example 1: floatval('150.03_page-section');
  // *     returns 1: 150.03
  // *     example 2: floatval('page: 3');
  // *     returns 2: 0
  // *     example 2: floatval('-50 + 8');
  // *     returns 2: -50
  return (parseFloat(mixed_var) || 0);
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/var/floatval.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/var/floatval.js)

### Example 1
This code
{% codeblock lang:js example %}
floatval('150.03_page-section');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
150.03
{% endcodeblock %}

### Example 2
This code
{% codeblock lang:js example %}
floatval('page: 3');
floatval('-50 + 8');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
0
-50
{% endcodeblock %}


### Other PHP functions in the var extension
{% render_partial _includes/custom/var.html %}
