Advanced Search

Results 1 to 5 of 5

Thread: Convert String abbreviation value to numbers

  1. #1
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question 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!
    Last edited by bigalo; 04-01-2008 at 07:25 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    Does this help?:
    Code:
    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!
    Jeremy | jfein.net

  3. #3
    Join Date
    Aug 2007
    Location
    Ohio
    Posts
    79
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Default

    Code:
    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);
    Last edited by jackbenimble4; 03-29-2008 at 02:35 PM. Reason: errors in code

  4. #4
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default

    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!

  5. #5
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default

    Thanks Nile!

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


    Thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •