Cpanel-JSON-XS icon indicating copy to clipboard operation
Cpanel-JSON-XS copied to clipboard

force non-scientific notation of numbers

Open freyfogle opened this issue 8 years ago • 3 comments

I have a number like -4.43e-05 in Perl.

I want to produce JSON like "longitude" : -0.0000443, (decimal notation)

but if I do encode_json [-4.43e-05] I get -4.43e-05 (scientific notation).

If I first format my number with something like sprintf('%.7f',$num) to take it out of scientific notation, the result is a string, not a number, and I then get JSON like "longitude" : "-0.0000443",.

I can't seem to find a way to force numbers in the JSON output (as opposed to strings) to be represented in decimal notation. Am I overlooking something?

Thanks for your help.

freyfogle avatar Feb 28 '17 01:02 freyfogle

I'm afraid, but you cannot do that. JSON::XS will aways return the result of the internal number conversion Gconvert (or quadmath_snprintf), which usually is %g, definable in config.h. Only inf/nan are exceptions.

You might try to persuade upstream JSON to add such a feature.

rurban avatar Feb 28 '17 18:02 rurban

thanks for the fast answer, even if it's not what i was hoping to hear

freyfogle avatar Feb 28 '17 18:02 freyfogle

I leave it open, as it really is an upstream decision to add a custom number formatter hook. If JSON decides to add such an option, Cpanel::JSON::XS will happily follow. Please add a link to this ticket here, if you open a ticket there. (url is enough)

rurban avatar Feb 28 '17 18:02 rurban