util.format() leading zeros

1,792 views
Skip to first unread message

Thijs Koerselman

unread,
Mar 27, 2012, 8:22:47 AM3/27/12
to nod...@googlegroups.com
Is it possible to write something like this?

util.format("page_%02d", index);

The above doesn't work, and I can't find anything in the docs about leading zero's.

Ben Noordhuis

unread,
Mar 27, 2012, 8:26:59 AM3/27/12
to nod...@googlegroups.com
On Tue, Mar 27, 2012 at 14:22, Thijs Koerselman
<thijsko...@gmail.com> wrote:
> Is it possible to write something like this?
>
> util.format("page_%02d", index);

Alas, no. util.format() is intentionally as brain dead as possible.

Mark Hahn

unread,
Mar 27, 2012, 1:25:13 PM3/27/12
to nod...@googlegroups.com
I recommend underscore_string.


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Brandon Benvie

unread,
Mar 29, 2012, 7:39:33 AM3/29/12
to nod...@googlegroups.com
("0000" + someNumber).slice(-4)

Adrian Lynch

unread,
Oct 24, 2014, 7:25:42 AM10/24/14
to nod...@googlegroups.com
Wrapped in a function and accounting for the number being longer than the mask:

function numFmt(num, mask) {
return (mask + num).slice(-Math.max(mask.length, (num + "").length));
};

Thanks Brandon, 2 1/2 years later :P

Adrian

On Thursday, 29 March 2012 12:39:33 UTC+1, Brandon Benvie wrote:
("0000" + someNumber).slice(-4)

Reply all
Reply to author
Forward
0 new messages