Hello all,
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.
Basically, you can use a regular expression for this:
<script>
a = "foo 12.34 bar 56 baz 78.90";
numbers = a.match(/\\d+\\.?\\d*/g)
alert(numbers)
</script>
It this snippet, “numbers” is an array containing all numbers found in given string.
vgarcia
February 15, 2005, 3:18pm
3
Couldn’t parseFloat() also work for this scenario?
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…
Thks.
7stud
February 15, 2005, 5:27pm
5
If you paste the following line into any browser’s address bar, what do you get?
javascript:alert(parseFloat(“US$2.40”));
You on a hiking trip in the Himilayas or something?
7stud
February 15, 2005, 5:29pm
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
alert(typeof(str));