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

float specifier maxes out at 20.

Open daurnimator opened this issue 8 years ago • 6 comments

e.g.

> require("sprintf").sprintf("%.99f", 1)
RangeError: toFixed() digits argument must be between 0 and 20
    at Number.toFixed (<anonymous>)
    at str_format.format (./node_modules/sprintf/lib/sprintf.js:170:49)

daurnimator avatar Jun 18 '17 14:06 daurnimator

Yes

alexei avatar Jun 18 '17 15:06 alexei

Even in C89 sprintf has no such limitation (well it did have max 99). If you don't want to implement it, then it at least needs a warning in the docs.

daurnimator avatar Jun 18 '17 15:06 daurnimator

This is a JS implementation and works inside JS limits so I don't think this is warranted.

As a side note, if one's using JavaScript and is bloody serious about the number of digits after the decimal point and finds that 20 digits are insufficient, they're probably using some decimal type. Since this particular implementation has no support for custom types or custom formatting methods, they're probably not using this library either. That, or they print it out as a string and be done with it.

alexei avatar Jun 18 '17 16:06 alexei

Otoh what might work would be to do away with the type casting. Then one might be able to implement their own toFixed, toPrecision etc. methods on their own special "types".

That would also mean users would have to pass arguments with the appropriate types (i.e. pass float when they mean float etc)

Even though the chance of having someone complain the next day about me breaking their app is pretty high, ~that's~ that change (dropping type casting) is something I'd think about and probably agree with 😊

Opinions?

alexei avatar Jun 18 '17 16:06 alexei

I updated my previous comment to make it clear that I'd probably agree with forcing users to pass arguments with the appropriate type.

alexei avatar Jun 18 '17 16:06 alexei

The issue I have is that I'm sharing sprintf strings between a C and a JS codebase: %.99f works in C, but not in JS.

daurnimator avatar Jun 27 '17 04:06 daurnimator