Due to suspected platform differences between developers, json.dump
is formatting scientific notations in different ways (one person’s machine, it formats to 1e-6
, and on others it formats to 1e-06
). The files are committed to our git history, so having to constantly revert the changes is annoying.
Is there a way to control how scientific numbers are formatted?
2
Answers
Do you need to use the built-in JSON module? yyjson has more control over number parsing, including support for Decimal objects which will preserve this regardless of platform. Plus, it’s just much faster.
If you’re reading huge floats/doubles or require perfect precision, you can
tell yyjson to read all numbers as Decimals:
Or use
ReaderFlags.BIG_NUMBERS_AS_DECIMAL
to only read numbers that aretoo large for Python’s float type as Decimals:
This will round-trip regardless of platform.