Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

How to get the number from the middle of a string


Given the following string extract the number

 'Buy for $199.99' // 199.99
 'Gas Price $2.304 per gallon' // 2.304

We can solve this using slice and, parseFloat().

  • slice returns the string from the starting index until the end of the string.

  • parseFloat parses until it finds the non-numeric character.

parseFloat( 'Buy for $199.99'.slice(9) ) // 199.99
parseFloat( 'Gas Price $2.304 per gallon'.slice(11) ) // 2.304

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *