Log in

View Full Version : Convert String abbreviation value to numbers



bigalo
04-01-2008, 06:48 PM
Hello,

Does anyone know if there is a script that converts a string value to a number?

Heres what I'm trying to do;
If you have a form text field that warrants a dollar value to be entered like "6,000,000,000, 6,000,000 or 6,000". I would like to make it available for the user to enter an abreviation of "6b, 6m or 6th" in the form field and once it's entered, it changes the letter abreviation (b, m or th) to the full numeric value (,000,000,000 or ,000,000 or ,000).

ex: 6b=6,000,000,000 / 6m=6,000,000 / 6th=6,000

Can anyone help with this?
Thanks!

Medyman
04-02-2008, 01:03 PM
Hi Bigalo,

First of all, I'm not a big javascript guy so this won't be the complete answer, but it should get you started.

I can easily do what you want in PHP with the str_replace function -- bascially swap out each instance of a set string with another.

Now, there is a similar method in JavaScript -- the replace() method (http://www.w3schools.com/jsref/jsref_replace.asp)


stringObject.replace(findstring,newstring)

Now, you could use that for your string replacement. Then you would use the DOM to write the results of this method back into your textbox/area.

I also imagine you'll need some sort of triggering this method through a key down function or something of the sort.

Sorry that I can't be of much help but I saw no one had chimed in yet so I thought I'd get you started. Maybe try to get something going with my info above and then post in the javascript forum for some additional help (once you have something to show).

HTH :)

bigalo
04-02-2008, 02:10 PM
Thanks Medyman,

I will give that a shot and see what I come up with.