8000 Floating Point Precision for Protobufs isn't intuitive · Issue #390 · googleapis/proto-plus-python · GitHub
[go: up one dir, main page]

Skip to content
Floating Point Precision for Protobufs isn't intuitive  #390
Open
@holtskinner

Description

@holtskinner

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0