How to extract decimal number from string using JavaScript

20,782

Solution 1

You can use regex like this,

Live Demo

var amount = "$265.12+";
var doublenumber = Number(amount.replace(/[^0-9\.]+/g,""));

Solution 2

Without error checks, following will do:

var string = "$123123.0980soigfusofui"
var number = parseFloat(string.match(/[\d\.]+/))

123123.098

Share:
20,782
Admin
Author by

Admin

Updated on October 12, 2021

Comments

  • Admin
    Admin over 2 years

    I want to extract decimal number 265.12 or 0.0 from alphanumeric string (say amount) having value $265.12+ or $265.12- or $0.0+ or $0.0- and use it apply struts logic tag in JSP. Not sure how to extract number maybe with help of JavaScript.

  • Oleg V. Volkov
    Oleg V. Volkov about 12 years
    How could he when he needs to extract, not print?
  • 250R
    250R about 12 years
    there's a method called unformat() that returns number. E.g. accounting.unformat("$ 12,345,678.90"); // 12345678.9
  • Ruben Murray
    Ruben Murray almost 4 years
    I believe the regex can be shortened to /[\d.]+/ - stumbled upon that in JS