Open
Description
Floating Point precision is based on the number of total digits, not digits after the decimal point, as most other libraries use.
"""
float_precision (Optional(int)): If set, use this to specify float field valid digits.
Default is None.
Returns:
str: The json string representation of the protocol buffer.
"""
return MessageToJson(
cls.pb(instance),
use_integers_for_enums=use_integers_for_enums,
including_default_value_fields=including_default_value_fields,
preserving_proto_field_name=preserving_proto_field_name,
sort_keys=sort_keys,
indent=indent,
float_precision=float_precision,
)
Example
class Sq
6235
uid(proto.Message):
mass_kg = proto.Field(proto.FLOAT, number=1)
s = Squid(mass_kg=3.14159265)
s_dict = Squid.to_dict(s, float_precision=3)
assert s_dict["mass_kg"] == 3.14
I would expect float_precision=3
to set the mass_kg
to 3.141
instead of 3.14
(3 decimal places instead of 2)