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

6 Answers 6

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);
Sign up to request clarification or add additional context in comments.

8 Comments

Your regex is quite... broad.
+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
thanks, I still have a problem though. If the string is: "$1.99." I get "1.99.". Is there an easy solution for this?
@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.
@32bitkid, I edited it to use 0.5.toLocaleString() to normalize fraction separators and myriad separators before converting to a number.
|
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

Comments

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]);

Comments

1

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

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

Comments

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

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

Comments

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));
}

3 Comments

Read the question more closely.
neither of these work with the data provided. you just read the title, didn't you.
@RobW: Are you referring to the loop part or something else?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.