[Javascript] Number formatting

Paul Novitski paul at juniperwebcraft.com
Thu Jun 29 14:52:50 CDT 2006


At 12:26 PM 6/29/2006, Liam Rice wrote:
>I've got a set of user input fields where a user is able to enter a
>number.  The user can enter with or without commas/spaces for
>separators.  I'm trying to remove them, but I keep getting the error
>iNum.replace is not a function.  Anybody have any ideas?
>
>function cleanNumber(iNum) {
>         iNum = iNum.replace(",","");
>         iNum = iNum.replace(' ','');
>         iNum = parseFloat(iNum);
>
>         return iNum;
>}


Since .replace() is a string function, try converting the number to a 
string first:

         sNum = iNum.toString;
         sNum = sNum.replace(",", "");
         ...
         iNum = parseFloat(sNum);

http://docs.sun.com/source/816-6408-10/object.htm#1193350
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Predefined_Core_Objects:Number_Object  




More information about the Javascript mailing list