Skip to content

Instantly share code, notes, and snippets.

@JDMcKinstry
Created February 25, 2016 20:54
Show Gist options
  • Select an option

  • Save JDMcKinstry/d4fccec14223eb021c3e to your computer and use it in GitHub Desktop.

Select an option

Save JDMcKinstry/d4fccec14223eb021c3e to your computer and use it in GitHub Desktop.
Convert string to Int || Float
if (Object['defineProperty'] && !String.prototype.hasOwnProperty('toInt')) Object.defineProperty(String.prototype, 'toInt', { value: function() { return parseFloat(this.replace(/[^0-9|.]/g, '').replace(/^0+/g, '')); } });
// Get any string into an INT or FLOAT, if it has a period
"jafs98y934hrqrjn34.wf.4e3whtw0g78sbenh bsghers.".toInt() // 9893434
"a1b2@&%92.234dfg.64%^564j.thyj5.67!~56.3453 v3".toInt() // 1292.234
"55.66.77".toInt(); // 55.66
"$4.55".toInt(); // 4.55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment