ajson icon indicating copy to clipboard operation
ajson copied to clipboard

Setting numbers (floats with precision)

Open mbtools opened this issue 5 years ago • 3 comments

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.

mbtools avatar Sep 09 '20 02:09 mbtools

yeah, good point. set_number would do, with optional precision param. Maybe also default precision on instance level too.

sbcgua avatar Sep 09 '20 19:09 sbcgua

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 ? =))))

sbcgua avatar Sep 09 '20 19:09 sbcgua

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.

amici avatar Sep 20 '24 10:09 amici

@mbtools https://github.com/sbcgua/ajson/commit/d6df9af55b8cc91fe1ba58e2ebe43019151517c8 - mmm?

sbcgua avatar Oct 21 '24 18:10 sbcgua

perfect

mbtools avatar Oct 21 '24 20:10 mbtools