Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Mel Smith" Newsgroups: comp.lang.javascript Subject: Placing 'zeros' in front of a number ? Date: Thu, 21 Jun 2012 18:37:43 -0600 Lines: 37 Message-ID: X-Trace: individual.net crPGDPlJk2YYvSQOJKu47ARbQcdo/cP1V28546iLh3mLB8529uiUAyXAT3FeB8ezw8 Cancel-Lock: sha1:R3Nd8L+dCRNnylwm9gspzcyDQik= X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 X-RFC2646: Format=Flowed; Original Xref: csiph.com comp.lang.javascript:14608 Hi: I've got a bunch of numbers (in string format), that I want to convert to strings of a standard length of four characters: e.g., '1' >>> '0001' '10' >>> '0010' '123' >>> '0123' '4567' >>> '4567' // leave as is for length of 4 I would like a general javascript function to convert the strings on the left above to those on the right -- for a variety of widths. I do this now but in an embarassingly slow and clumsy way. In my native language (Harbour), we have a function called strzero() which is used something like this: cMyStringifiedNumber = "123" cMyNewStringifiednumber := STRZERO(val(cMyStringifiedNumber),4,0) // the val() function converts a string to a number // then the strzero() function operates on the number and returns a new stringified number // and where the '4' indicates the total width of the needed string // and the the ',0' meand no decimal points needed in the result. // the result for the above is '0123' //. that is, a '0' is placed at the front of the string Has anybody got a generalized function that can do the above please ?? Thank you ! -- Mel Smith