Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature to format floating point numbers #896

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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);
        }
    }

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