Skip to content

Conversation

Vanmaele
Copy link

This work allows to format floating point, either set the decimal point or significant number. If not format preference are given then the driver behaves as before.

Note that JSON specifies to print numbers in it's shortest form, this is still the case e.g. but a number will not be printed longer than e.g. a given decimal point. But a number that has been formatted with a decimal point won't be printed in a scientific notation.

@Tvalley71
Copy link

The length needs to be adjusted if trailing zeros are removed or else a 0-termination is copied into the output buffer.

    else if (item->type & cJSON_NumberIsFormatted)
    {
        precision = _get_precision_from_item(item, sizeof(number_buffer));
        if (item->type & cJSON_NumberFormatStyleFixedPoint)
        {
            sprintf((char*)number_buffer, "%.*f", (int)precision, d);
            remove_trailing_zeros((char*)number_buffer);
            length = strlen((char*)number_buffer); /*     <================    */
        }
        else
        {
            length = sprintf((char*)number_buffer, "%.*g", (int)precision, d);
        }
    }

The length needs to be adjusted if trailing zeros are removed or else a 0-termination is copied into the output buffer.
@Vanmaele
Copy link
Author

Thank you for the review. I've updated it as suggested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants