tags:

views:

279

answers:

5

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?

A: 

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( "%f", &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.

Goz
Im trying to round a number that is 3 decimal places long.Lets say I input 5.092697436I want that to round to 5.10
Chandler
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 places5.698The number you have entered is 0.05
Chandler
Use %lf for scanning doubles. %f only works with floats.
hrnt
I thought %f was for doubles and floats. I thought internally printf used doubles, or is that a windows only thing?
Goz
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.
hrnt
A: 

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

Tim Sylvester
So is that where my problem is? I should have %f instead of %d?
Chandler
@Chandler Yes, if the variable you're printing is of type `double`, you should use "%f".
Tim Sylvester
+2  A: 

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.

hrnt
So what is an alternative?
Chandler
Added a suggestion
hrnt
should i use the pow() function then in that case?
Chandler
Use pow() for what?
hrnt
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?
Chandler
To round to 2 places multiple by 100 (ie 10^2) chop off the fractional part, then divide by 100 again
Martin Beckett
A: 

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);
mobrule
A: 

{

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

Chandler