A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Remove $ and , from a number.

  1. #1

    Remove $ and , from a number.

    I have been racking my brain to figure this one out.

    I have a number that I have formatted as dollars.

    Ex. $20,000.00

    I am tring to make it so that I can take that number and strip out the $ and , so then I can add them together then I'll reformat it as dollars again. The number will always be different and could be any #, so I want to dynamically strip out any $ and , so it looks like this: 20000.00. Any ideas for striping or extracting certian characters?

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Here's some stripping code. The technique is to loop thru the string and use String.substr() to examine individual characters.

    You will also need a function to reformat the result. I can provide that if you need it.

    code:

    // Given a string such as "$20,000.47"
    // return the numeric equivalent

    stripNumber = function(s)
    {
    var d = '';
    var len = s.length;
    for (i = 0; i < len; ++i)
    {
    var ch = s.substr(i,1);
    switch (ch) {
    case '.':
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    d += ch;
    break;
    }
    }
    // trace(d);
    return Number(d);
    }

    // Example Usage
    x = "$20,000.00";
    y = "$30,000.47";

    z = stripNumber(x) + stripNumber(y);

    trace(z);

    // Reformat Z to use dollars and commas here...


  3. #3
    Thank you that works perfect. Just as fyi I found this fuctions to format as dollars as a tech note that works pretty well.

    Code:
    function formatAsDollars(amount, isCents) {
    	if (isNan(amount)) {
    		return NaN;
    	}
    	if (isCents != "isCents") {
    		amount = amount*100;
    		amount = Math.round(amount);
    	}
    	amount = String(Math.floor(amount));
    	if (amount.length == 1) {
    		return ("$0.0"+amount);
    	}
    	if (amount.length == 2) {
    		return ("$0."+amount);
    	}
    	cents = amount.slice(-2);
    	amount = amount.substring(0, amount.length-2);
    	dollars = [];
    	do {
    		dollars.push(amount.slice(-3));
    		amount = amount.substring(0, amount.length-3);
    	} while (amount.length>3);
    	if (amount.length) {
    		dollars.push(amount);
    	}
    	dollars.reverse();
    	return ("$"+dollars.join()+"."+cents);
    }
    Now I take the previously dollar formatted text and pass it through the function you provided then do my calculation then pass it back through the formatAsDollars function. This is exactly what I needed.

  4. #4
    professional amateur
    Join Date
    Apr 2001
    Location
    Amsterdam
    Posts
    187
    I'd use this as function :

    code:

    stripNumber = function(s)
    {
    s = s.split("$").join("");
    s = s.split(",").join("");
    return parseFloat(s);
    }



    split at the '$' , replace with nothing and do the same for the ','

    might be enough in your case ?
    Last edited by jbum; 10-19-2004 at 04:23 PM.
    back to work at flashclub.nl.
    Ngezi


  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Yes, NGezi's method is better - also about twice as fast.

    - Jim

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center