Is it possible to round a double using just a printf statement?

17,066

Solution 1

You really, really should not store "rounded" values in floating point variables. Floating point inaccuracy will ruin this - your 5.10 might become 5.099999999941892 simply because the implementation might not be able to store 5.10 exactly.

As an alternative, read the whole number, multiply it with 100 and convert it to int (which will round it towards zero). That will keep your calculations accurate.

Solution 2

"%.2f" will round a double to 2 digits. A double is not an integer, and %d and %f are not interchangeable.

Solution 3

{

float x;
float rounded_x;

printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%f", &x);
rounded_x = ((int)(x * 100 + .5) / 100.0);

printf( "The number you have entered is %.2f\n", rounded_x);
return 0;

}

Thank you to everyone who tried to help! I finally got it

Share:
17,066
Chandler
Author by

Chandler

Updated on June 04, 2022

Comments

  • Chandler
    Chandler almost 2 years

    Obviously this is just a fraction of the code.

    printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
    scanf("%5.2d", &x);
    printf("The number you have entered is %5.2d\n" ,x);
    

    Would this automatically round the number I type in? Or is there another way to do this?

    Edit:

    printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
    scanf("%lf", &x);
    x = x + 0.05;
    printf( "The number you have entered is %5.2lf\n", x);
    

    Iv done this, but Im taking into consideration what someone had said about printf just "changing" the way it reads out. So this is obviously not the right way. Should I implement maybe the pow() function? Will that work with this somehow?

    Edit2:

    printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
    scanf("%lf", &x);
    x = x + 0.05;
    printf( "The number you have entered is %5.2lf\n", x);
    

    Okay, iv gotten to the point where if i imput a number it will round to a whole number. 35.21 will round to 35, and 35.51 will round to 36 et. etc.

    How would I get 35.2178 to round to 35.22, and 35.2135 to round to 35.21. How would I get the certain powers of the decimal to round instead of the whole number?

    • Chandler
      Chandler over 14 years
      I have tried it, and it only works some of the time. Is there another way of going about doing this? I'm relatively new to C.
    • palantus
      palantus over 14 years
      Then you shouldn't have said it was an integer. Or did you mean "round to an integer"?
    • Chandler
      Chandler over 14 years
      I dont understand, where did I say it was an integer? Does %d not work with this?
    • u0b34a0f6ae
      u0b34a0f6ae over 14 years
      %d is for an integer. Where did you learn it was for doubles? Perhaps you should look into a reference or book.
    • hrnt
      hrnt over 14 years
      You can't use %.2f with scanf.
    • pmg
      pmg over 14 years
      The format string in scanf() is different than in printf(). The one in your example is just plain wrong. To read a double from stdin, use scanf() with a "%lf" format string, then round the number in any way you prefer.
    • Chandler
      Chandler over 14 years
      yes but how will the rounding occur? By using %5.2lf?
  • Chandler
    Chandler over 14 years
    Im trying to round a number that is 3 decimal places long. Lets say I input 5.092697436 I want that to round to 5.10
  • Chandler
    Chandler over 14 years
    So is that where my problem is? I should have %f instead of %d?
  • Chandler
    Chandler over 14 years
    Alright I tried this, but it keeps spitting .05 back at me. Please enter a positive number that has a fractional part with three or more decimal places 5.698 The number you have entered is 0.05
  • Chandler
    Chandler over 14 years
    should i use the pow() function then in that case?
  • Goz
    Goz over 14 years
    I thought %f was for doubles and floats. I thought internally printf used doubles, or is that a windows only thing?
  • hrnt
    hrnt over 14 years
    Yes, %f is for doubles and floats with printf (because of the vararg argument promotion rules). For scanf, %f is for floats and %lf is for doubles.
  • Chandler
    Chandler over 14 years
    I've got it to round to whole numbers, I's just curious now as to how to round it to certain decimal places. Would I use the pow() function?
  • Martin Beckett
    Martin Beckett over 14 years
    To round to 2 places multiple by 100 (ie 10^2) chop off the fractional part, then divide by 100 again
  • Tim Sylvester
    Tim Sylvester over 14 years
    @Chandler Yes, if the variable you're printing is of type double, you should use "%f".