Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature to format floating point numbers - #896

Open
Vanmaele wants to merge 8 commits into
DaveGamble:masterfrom
2make-org:feature/number-float-formatting
Open

Vanmaele wants to merge 8 commits into
DaveGamble:masterfrom
2make-org:feature/number-float-formatting

Conversation

@Vanmaele

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown
Author

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

@Vanmaele

Vanmaele commented May 8, 2026

Copy link
Copy Markdown
Author

Another project recently showed interest in this functionality, so I wanted to check whether this is something that could be considered for merge, or whether there are architectural concerns/preferences I should address first.

I've also been using this feature in my fork for quite some time now without issues, so I'm mainly trying to understand whether it makes sense to keep waiting for upstream inclusion, or whether the preference would be to close/reject the PR.

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