sprintf.js icon indicating copy to clipboard operation
sprintf.js copied to clipboard

Does not support `%.*f` format

Open acg opened this issue 9 years ago • 3 comments

This syntax lets you specify the precision with an argument.

> require("sprintf-js").sprintf("%.*f", 2, 3.14151)
SyntaxError: [sprintf] unexpected placeholder

From sprintf(3):

The precision
       An optional precision, in the form of a period ('.')   followed
       by  an  optional  decimal  digit  string.  Instead of a decimal
       digit string one may write "*" or "*m$" (for some decimal inte-
       ger m) to specify that the precision is given in the next argu-
       ment, or in the m-th argument, respectively, which must  be  of
       type int.  If the precision is given as just '.', the precision
       is taken to be zero.  A negative precision is taken as  if  the
       precision  were omitted.  This gives the minimum number of dig-
       its to appear for d, i, o, u, x, and X conversions, the  number
       of  digits  to appear after the radix character for a, A, e, E,
       f, and F conversions, the maximum number of significant  digits
       for g and G conversions, or the maximum number of characters to
       be printed from a string for s and S conversions.

The %.*m$f syntax probably isn't useful outside of C, so I wouldn't be sad if it was left out of sprintf-js.

acg avatar May 14 '16 23:05 acg

I ran into a similar problem with the variable width as well: the format "%*d" gives the same error.

costacosta avatar Dec 15 '16 04:12 costacosta

Incidentally we ran into this sort of issue with Python recently and ended up doing something like:

fmt = '%%%dd' % (width)
return fmt % (val)

which doesn't look like the sort of code you want to come back later.

I think it's hard to do this using the current parser (which I find ugly). I've been planning on implementing a different one but currently I don't have the time. I am sorry.

alexei avatar Dec 15 '16 10:12 alexei