How to extract decimal number from string using JavaScript
20,782
Solution 1
You can use regex like this,
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
Author by
Admin
Updated on October 12, 2021Comments
-
Admin over 2 years
I want to extract decimal number
265.12
or0.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 about 12 yearsHow could he when he needs to extract, not print?
-
250R about 12 yearsthere's a method called unformat() that returns number. E.g. accounting.unformat("$ 12,345,678.90"); // 12345678.9
-
Ruben Murray almost 4 yearsI believe the regex can be shortened to
/[\d.]+/
- stumbled upon that in JS