lisovaccaro
lisovaccaro

Reputation: 33956

Javascript get number from string?

I have an array containing strings with prices and sometimes surrounded by characters.

How do I transform it from?

[0] > '$9.99/aa'
[1] > '$2.99'
[2] > '$1.'

to:

[0] > '9.99'
[1] > '2.99'
[2] > '1'

So I can do comparisons with the values? I just need to know how to change one and I can apply it to the array easily

Upvotes: 0

Views: 3601

Answers (6)

Mike Samuel
Mike Samuel

Reputation: 120506

+myString.replace(/[^\d.ex-]+/gi, '')

strips out all characters that cannot appear in a JavaScript number, and then applies the + operator to it to convert it to a number. If you don't have numbers in hex format or exponential format then you can do without the ex.

EDIT:

To handle locales, and handle numbers in a more tailored way, I would do the following

// Get rid of myriad separators and normalize the fraction separator.
if ((0.5).toLocaleString().indexOf(',') >= 0) {
  myString = myString.replace(/\./g, '').replace(/,/g, '.');
} else {
  myString = myString.replace(/,/g, '');
}

var numericValue = +(myString.match(
    // Matches JavaScript number literals excluding octal.
    /[+-]?(?:(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?|0x[0-9a-f]+)/i)
    // Will produce NaN if there's no match.
    || NaN);

Upvotes: 2

georg
georg

Reputation: 214949

Strip everything before the first digit and apply parseFloat to the rest:

s = "$9.99/aa"
alert(parseFloat(s.replace(/^\D*/, '')))

Upvotes: 1

Justin Thomas
Justin Thomas

Reputation: 5848

parseFloat() works for such cases when you need the decimals. The regex will give the matched number for all your cases in the 0 index. Sample code is below.

var one = "2.99";
var two = '$1.';
var three = '$3tees';
var four = '$44.10'    

var regex = /\d+\.?(\d+)?/;
var num = parseFloat(one.match(regex)[0]);

Upvotes: 1

Hello71
Hello71

Reputation: 816

The lazy way:

+string;

The proper way:

parseInt(string, 10);

Thus:

for (var i = 0; i < arr.length; i++) {
    var match = /\$(\d*)\.(\d\d)/.exec(arr[i]);
    arr[i] = parseInt(match[1] + match[2]);
}

Edit: Oh, you wanted something more than 10 dollars.

Another edit: Apparently parseInt (at least in my browser) ignores trailing characters. In that case, my original implementation would have worked.

for (var i = 0; i < arr.length; i++) {
    arr[i] = parseInt(arr[i].substring(1));
}

Upvotes: 0

dorsh
dorsh

Reputation: 24690

+/[\d\.]+/.exec('$9.99/aa')[0]

Finds the first group of digits and periods, converting them to a float

Upvotes: 0

Rob W
Rob W

Reputation: 348992

Your case requires a Regular Expression, because all native number-converting methods fail when the string is prefixed by a non-digit/dot.

var string = '$1.22'; //Example
string = string.replace(/[^0-9.]+/g, '');
// string = '1.22'

If you want to convert this string to a digit, afterwards, you can use parseInt, +, 1*.

For a comparison of these number-converting methods, see this answer

Upvotes: 2

Related Questions