0

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

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

6 Answers6

2
+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);
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 1
    Your regex is quite... broad. – Hello71 Jan 18 '12 at 22:08
  • +1 the only thing I would add is I don't think this conversion (or any of the float conversions in js) are locale dependent. just an FYI – J. Holmes Jan 18 '12 at 22:10
  • thanks, I still have a problem though. If the string is: "$1.99." I get "1.99.". Is there an easy solution for this? – lisovaccaro Jan 18 '12 at 22:22
  • @Hello71, Liso22, you can narrow the regular expression by just looking for the number grammar. I edited it to find valid numbers and to expect locale sensitive fraction separators. – Mike Samuel Jan 18 '12 at 22:39
  • @32bitkid, I edited it to use `0.5.toLocaleString()` to normalize fraction separators and myriad separators before converting to a number. – Mike Samuel Jan 18 '12 at 22:44
  • @MikeSamuel: I don't think that works. You may need to use `(0.5).toLocaleString()`. – Hello71 Jan 19 '12 at 21:45
  • Huh. Apparently not (in Firefox, anyways). – Hello71 Jan 19 '12 at 21:46
  • @Hello71, it's not required. `0.toLocaleString` won't work because it is tokenized as `0.` followed by `toLocaleString`, but `0.5.toLocaleString` is tokenized as `0.5` then `.` then `toLocaleString`. Edited since it's potentially confusing. – Mike Samuel Jan 19 '12 at 21:50
2

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

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
1

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]);
Justin Thomas
  • 5,680
  • 3
  • 38
  • 63
1

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

s = "$9.99/aa"
alert(parseFloat(s.replace(/^\D*/, '')))
georg
  • 211,518
  • 52
  • 313
  • 390
0

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));
}
Hello71
  • 766
  • 8
  • 17
0
+/[\d\.]+/.exec('$9.99/aa')[0]

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

dorsh
  • 23,750
  • 2
  • 27
  • 29