Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:27:38+00:00 2026-06-17T19:27:38+00:00

Originally I was using sprintf with floats always with 2 decimal places using the

  • 0

Originally I was using sprintf with floats always with 2 decimal places using the following code:

static void MyFunc(char* buffer, const float percentage)
{
    sprintf(buffer, "%.2f", percentage);
}

One of the percentage values passed was 0x419FFFFF 20 (debugger view), this printed 20.00 into buffer.

I would like instead to show 2 decimal places when not an integer, e.g.

94.74 displayed as 94.74
94.7  displayed as 94.70
0     displayed as 0
5     displayed as 5
100   displayed as 100

I am currently using the following code:

static void MyFunc(char* buffer, const float percentage)
{
    int fractional_part = ((percentage - (int)percentage) * 100);
    if (0 == fractional_part)
    {
        sprintf(buffer, "%d", (int)percentage);
    }
    else
    {
        sprintf(buffer, "%.2f", percentage);
    }
}

Now if 0x419FFFFF 20 (debugger view) is passed, fractional part is calculated as 99. I assume then the sum for fractional_part ends up being (19.99 – 19) * 100 = 99. Why then does the first example not print 19.99 into buffer?

What is the correct solution for my problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:27:40+00:00Added an answer on June 17, 2026 at 7:27 pm

    Yours is a problem of approximation.

    Suppose that the percentage is 19.999. Then fractional_part would be 99, and the floating point branch would be invoked.

    But printing 19.999 with two decimals will round it to 20.00, and that is what is printed.

    You could always use the floating point branch, in order to get consistent results, and then truncate at ‘.’ if it comes out with ‘.00’. Otherwise, you risk your test and printf‘s internals to be at odds some time.

    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
            float percentage = 19.999;
            char buffer[50];
    
            for (percentage = 19.990; percentage < 20.001; percentage += 0.001)
            {
                    sprintf(buffer, "%.2f", percentage);
                    char *p = strstr(buffer, ".00");
                    if (p) *p = 0x0;
                    printf("%.3f rendered as %.2f and becomes %s\n", percentage, percentage, buffer);
            }
            return 0;
    }
    
    19.990 rendered as 19.99 and becomes 19.99
    19.991 rendered as 19.99 and becomes 19.99
    19.992 rendered as 19.99 and becomes 19.99
    19.993 rendered as 19.99 and becomes 19.99
    19.994 rendered as 19.99 and becomes 19.99
    19.995 rendered as 19.99 and becomes 19.99
    19.996 rendered as 20.00 and becomes 20
    19.997 rendered as 20.00 and becomes 20
    19.998 rendered as 20.00 and becomes 20
    19.999 rendered as 20.00 and becomes 20
    20.000 rendered as 20.00 and becomes 20
    20.001 rendered as 20.00 and becomes 20
    

    If you don’t agree with printf‘s rounding strategy, just use round() on (a copy of) percentage and force your own. Or you might also, e.g., sprintf() with three digits, and erase the third.

    And in your specific case (note how my system (Linux x86_64) renders 0x419FFFFF):

    #include <stdio.h>
    #include <string.h>
    #include <stdint.h>
    
    int main(int argc, char **argv)
    {
            float percentage = 3.1415;
            char buffer[50];
    
            ((uint32_t *)(&percentage))[0] = 0x419FFFFF;
    
            sprintf(buffer, "%.2f", percentage);
            char *p = strstr(buffer, ".00");
            if (p) *p = 0x0;
            printf("%.15f rendered as %.2f and becomes %s\n", percentage, percentage, buffer);
            return 0;
    }
    
    
    19.999998092651367 rendered as 20.00 and becomes 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was originally using the following code: background-image: url(../images/tabbottom.gif); background-repeat: no-repeat; background-position: left bottom;
I've been refactoring some code that was originally using the Messenger in MVVM Foundation
I'm attempting to avoid using fixed buffer sizes with things like sprintf and friends
I was originally using SQLCE first starting with LINQ to SQL and then moved
In Fedora15, I was originally using vim , with all my settings defined in
I'm using MinGW (originally installed with mingw-get-inst-20120426.exe) in combination with Eclipse for C/C++ (Indigo
I originally setup some conditions using CGRectIntersectsRect for some collision detection which worked fine.
First time using Asp.net-mvc and originally followed the NerdDinner tutorial. My form submit button
I'm maintaining an MFC/COM/ATL 45-odd project solution originally written with VS6. I'm now using
This question was originally using MySQL 5.1.44, but is applicable to MySQL 8.0+ too.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.