Chandler
Chandler

Reputation: 45

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

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?

Upvotes: 1

Views: 19172

Answers (5)

Chandler
Chandler

Reputation: 45

{

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

Upvotes: 2

Goz
Goz

Reputation: 62333

This doesn't make sense

scanf("%5.2d", &x);

You can't have an integer with numbers after the decmal point. if x is a flat then weird things will happen. If its an integer why are you after 2 decimal places in the printf.

What exactly are you trying to do?

Edit:

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

I'm pretty sure printf only truncates. So you will need to add 0.005 to round it.

Upvotes: 0

mob
mob

Reputation: 118665

printf won't change the value of the number, just how it is displayed. An alternative is

#include <math.h>

// round  double x  to 2 decimal places
x = 0.01 * floor(x * 100.0 + 0.5);

Upvotes: 0

hrnt
hrnt

Reputation: 10142

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.

Upvotes: 5

Tim Sylvester
Tim Sylvester

Reputation: 23168

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

Upvotes: 3

Related Questions