get 1st float ( or int ) from given string


/ Published in: JavaScript
Save to your folder(s)

it parses given string and returns 1st number it encounters ( it will replace comas with dots so floats formatted as 12,345 could be 12.345 ( some countries use that ) )

then u can parse returned value as a number / float / int - do what You need with it ;-)


Copy this code and paste it in your HTML
  1. (/[\d.]+/).exec((__PROVIDE_STRING_TO_PARSE__).replace(",", "."))
  2.  
  3. // if u want it to make even more dumb-proof ( like multiple dots/comas "within" the number ) use this:
  4.  
  5. (/[\d]+.?[\d]+/).exec((__PROVIDE_STRING_TO_PARSE__).replace(",", "."))

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.