Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting number out of value

Status
Not open for further replies.

lahddah

Programmer
Jan 2, 2003
109
US
I have a drop down box with values similar to this:
"Railroad Bridge - Johnson Village - $45"

I am needing to perform calculations based on the price of this trip, therefore I would like to extract only the '45' portion of this string and use it in my calculations. All of the values for the drop down box are going to vary in length but will all have the numeric value at the end, so I just don't know how to grab the last 2 - 3 characters and make js see them as a number. I've tried variations of the 'index of' function and couldn't figure it out.

Suggestions?
Thank you!



~ lahddah
 
well, a simple answer to this is to set a value for each option, the value being the price, for example:
Code:
<option value='45'>Railroad Bridge - Johnson Village - $45

the other way, would be to get the option out of the select box and split it by the "$" ... assuming there is only one dollar sign in the option, this works:

Code:
<html>
<head>
<Script>
function getPrice(theOption)
{
parts = theOption.split('$');
alert ("The price is "+parts[1]+ " dollaz");
}
</script>
</head>
<body>
<form>
<select name=s1 onChange='getPrice(this.options[this.selectedIndex].innerText)'>
<option>el primero viaje -- $45
<option>the second trip -- $99
<option>and the third one -- $130
</select>
</form>
</body>
</html>

hope that helps



"Whether you think that you can, or that you can't, you are usually right." - Henry Ford
 
If there will always be a $ in front of the number, you can use something like this:
Code:
[b]function getDollars(str){
  return str.substring(str.lastIndexOf("$")+1);
}[/b]

var x = "Railroad Bridge - Johnson Village - $45"
alert(getDollars(x));

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Thanks, Dookie and Adam - so, being ignorant, apparently, here is my code - I'm not sure where to put everything (like is 'substring' really substring or is it 'document.vehicle_pickup.value' or something else)
-------------------------
Here's the JS w/ Adams code:

<SCRIPT LANGUAGE="JavaScript1.2">
function getDollars(str){
return str.substring(str.lastIndexOf("$")+1);
}

var x = "Railroad Bridge - Johnson Village - $45"
alert(getDollars(x));
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript1.2">
<!-- hide contents from old browsers
function tally()
{
var NumVehicles, Total;
//Cost = 0;
var count=0;
for (count=0; count<5; count+=1) {
if (document.orderform.vehicle_number[count].checked) {
NumVehicles = count+1;
}
}
Total = parseFloat(parseInt(document.orderform.vehicle_pickup.value) * parseInt(NumVehicles));

document.orderform.Total.value = "$" + Total;
//document.orderform.GrandTotal.value = "$" + Grand_Total;
}

function dollar (amount)
{
amount = parseInt(amount * 100);
amount = parseFloat(amount/100);
if (((amount) == Math.floor(amount)) && ((amount - Math.floor (amount)) == 0))
{
amount = amount + ".00"
return amount;
}
if ( ((amount * 10) - Math.floor(amount * 10)) == 0)
{
amount = amount + "0";
return amount;
}
if ( ((amount * 100) - Math.floor(amount * 100)) == 0)
{
amount = amount;
return amount;
}
return amount;
}

//-->
</SCRIPT>

----------------------------

Here's the HTML Options I'm trying to get the $ amt out of:

<SELECT NAME="vehicle_pickup" SIZE="1" ONCHANGE="tally()">
<OPTION VALUE="0" SELECTED="SELECTED">Choose Location</OPTION>
<OPTION VALUE="Railroad Bridge - Johnson Village - $45">Granite - Numbers Put-In - $50</OPTION>
<OPTION VALUE="Railroad Bridge - Ruby Mountain - $45">Railroad Bridge - Ruby Mountain - $45</OPTION>
<OPTION VALUE="Railroad Bridge - Hecla Junction - $55">Railroad Bridge - Hecla Junction - $55</OPTION>
<OPTION VALUE="Railroad Bridge - Stone Bridge - $55">Railroad Bridge - Stone Bridge -$55</OPTION>
<OPTION VALUE="Buena Vista - Johnson Village - $30">Buena Vista - Johnson Village - $30</OPTION>
</SELECT>

~ lahddah
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top