ajson
ajson copied to clipboard
Setting numbers (floats with precision)
It does not seem possible to generate a json for
{ "value" : 789.34 }
set with a float will produce the following (which is the correct representation for the float).
{ "value" : 789.34000000000003 }
something like set_number for type "p" would be required
PS: PHP uses an ini setting to influence the precision https://www.php.net/manual/en/ini.core.php#ini.serialize-precision. Maybe a class attribute is an alternative for zcl_ajson.
yeah, good point. set_number would do, with optional precision param. Maybe also default precision on instance level too.
The number of significant digits stored while serializing floating point numbers. -1 means that an enhanced algorithm for rounding such numbers will be used.
... what's the heck is enhanced rounding algorithm ? =))))
The number of significant digits stored while serializing floating point numbers. -1 means that an enhanced algorithm for rounding such numbers will be used.
... what's the heck is enhanced rounding algorithm ? =))))
In the default php.ini (for 8.2), there is the best info I could find:
; When floats & doubles are serialized, store serialize_precision significant
; digits after the floating point. The default value ensures that when floats
; are decoded with unserialize, the data will remain the same.
; The value is also used for json_encode when encoding double values.
; If -1 is used, then dtoa mode 0 is used which automatically select the best
; precision.
serialize_precision = -1
So: If -1 is used, then dtoa mode 0 is used which automatically select the best precision.
@mbtools https://github.com/sbcgua/ajson/commit/d6df9af55b8cc91fe1ba58e2ebe43019151517c8 - mmm?
perfect