PDA

View Full Version : Convert String abbreviation value to numbers



bigalo
03-28-2008, 01:35 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!

Nile
03-28-2008, 10:28 PM
Does this help?:


var replace = "6b";
var start = document.something.value;
var start = myOldString.replace(replace, ",000,000,000);

If you need to do this over and over again just give the variables a unique id!

jackbenimble4
03-29-2008, 02:26 PM
var number = "6,000,000,000";
var abbrev = "";
var new_number = "";

// remove commas
number = number.replace(/\,/g, '');


if(number % 1000000000 == 0) {
// if number is divisible by 1 billlion,
abbrev = 'b';
new_number = number / 1000000000;
}
else if(number % 1000000 == 0) {
abbrev = 'm';
new_number = number / 1000000;
}
else if(number % 1000 == 0) {
abbrev = 'th';
new_number = number / 1000;
}

alert(new_number + abbrev);

bigalo
03-31-2008, 01:55 PM
Thanks jackbenimble4!

But, how would I apply this to the field? I've tried adding a function but it does not seem to work. Could you please elaborate on how to apply this?

Thanks!

bigalo
03-31-2008, 01:56 PM
Thanks Nile!

Could you please elaborate on how to apply this code to the field?


Thanks!