/    Sign up×
Community /Pin to ProfileBookmark

Convert String abbreviation value to numbers

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!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManApr 02.2008 — are you familiar with identifying a form input element on a webpage? for instance, if you want to read the current value of the form text element named "textField"?

lets say for instance you have a form named "myForm" and a text element (which will contain your number like 6b) named "textField". to access the element in javascript, you'd use something like:

document.myForm.textField.value

after accessing that value, you can assign it to a variable. from there, you can use regular expressions to grab the information, or use the "indexOf" method. regular expressions seem overwhelming, but are surprisingly easy once you understand the patterns and the syntax. but for the sake of simplicity, its easier to use "indexOf" to find information in a string.

so lets say your variable, once assigned, contains the string "6b". youd then write a function to find the b (or "m", or "th"). heres an example for "b":

if(my_text_field_variable_name.indexOf("b") != -1)
run_some_code()


the reason we check that it doesnt equal negative one is becuase indexOf literally means the numeric placeholder index of the string we've specified.

so, for clarification, say you wanted to find where the word "fun" was in the phrase "disfunctional":

var myString = "disfunctional"
alert(myString.indexOf("fun"))


this will alert the number "3", because the D in that string is the ZERO index. the I is 1, the S is then 2, and that makes the F in "fun" 3. indexOf simply finds the first occurance of the string (if it exists in its entirety in your word).

if the string is not found, like if we looked for "fun" in "drunk driving", it returns -1, which means the string was not found.

so now that you can find your B (or other string), you have to rewrite the variable. to do this, we can use the "substring()" method. it is explained here:

http://www.w3schools.com/jsref/jsref_substring.asp

for simplicity sake, we can assume that the first index in your number string ('6b' for instance) will always be a number. therefore, when you do the substring, hardcode the first index to be "0". then, you can use the number returned from the indexOf method as the end point. however, you have to subtract 1 (one) from the indexOf result so as not to include that actual character in the new string.

myNewString = myOldString.substring(0, variableContainingIndexOfResult - 1)

once youve created a new substring, you finally place that information back into the form using the document.myForm.textField example i used above. instead of getting the information and putting it into a variable, you would use that variable to set the form value.

if this doesnt work for you when you work it out for yourself, post what youve tried and it can easily be debugged.

good luck!
Copy linkTweet thisAlerts:
@bigaloauthorApr 02.2008 — Thanks Aaron! I will give this a shot.
×

Success!

Help @bigalo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...