[Javascript] Number formatting

Paul Novitski paul at juniperwebcraft.com
Thu Jun 29 15:30:41 CDT 2006


At 01:03 PM 6/29/2006, joel.goldstick at columbuswebmakers.com wrote:
>I was thinking Javascript thinks its a string.  So I googled this here:
>http://www.siteexperts.com/tips/functions/ts23/page3.asp
>
>I think you could change the regex to /[, ]/ and it will work for you
>
>Remove Commas
>
>A user may enter a number separated by commas. You may wish to 
>remove the commas before storing the number. A reformatting function 
>to do that would look like this:
>
>function removeCommas( strValue ) {
>   var objRegExp = /,/g; //search for commas globally
>   //replace all matches with empty strings
>   return strValue.replace(objRegExp,'');
>}


I believe it wasn't Liam's replacement logic that ran him aground, 
but rather the fact that he was probably passing his function a 
Number object to which he was attempting to apply a String object 
method.  Once that argument is converted to a string, I believe 
replace() would work, either using RegExp as you suggest or a string 
as Liam was doing.

P. 




More information about the Javascript mailing list