gunbound_boy — 2005-02-15T09:55:05-05:00 — #1
I need help with some simple programming.
I need a script to extract numbers out of a string.
For eg, this string contains the following fields:
$TOTAL (variable) = US$2.40 (string)
I need to remove the US$ so that when i submit the form it only passes 2.40 into the output.
Will appreciate any advise, cause I am not really good at this.
Thks in advance.
stereofrog — 2005-02-15T10:07:26-05:00 — #2
Basically, you can use a regular expression for this:
a = "foo 12.34 bar 56 baz 78.90";
numbers = a.match(/\\d+\\.?\\d*/g)
It this snippet, "numbers" is an array containing all numbers found in given string.
vgarcia — 2005-02-15T10:18:57-05:00 — #3
Couldn't parseFloat() also work for this scenario?
gunbound_boy — 2005-02-15T10:29:37-05:00 — #4
Thks for the reply.
Now I realised i got another problem.
I need to retrieve the number that is stored in php.
ie. <?php $TOTAL= US$2.40 ?>...
Then I need to send this number outside of the php brackets to another destination via a form.
I hope you guys get what I mean...
7stud — 2005-02-15T12:27:32-05:00 — #5
If you paste the following line into any browser's address bar, what do you get?
You on a hiking trip in the Himilayas or something?
7stud — 2005-02-15T12:29:42-05:00 — #6
You could also do this:
var str = "US$2.40";
str = str.substring(3); //gets the substring from index position 3 to the end
str = Number(str); //converts to a number