0

I am trying to print out the results of the round() function.

#include <stdio.h>
#include <math.h>

int main(void){
    float change = 4.2; //want change for $4.2

    printf("rounded %i",round(change*100));

}

error: format specifies type 'int' but the argument has type 'double'

The round(double) function returns an int, not a double, so I am confused as to why this error is occurring.

Thank you so much for taking a look at this!

0

As mentioned by @DinoCoderSaurus the information is in the manual.

The funtion round, is actually

double round(double x); 

You can also use roundf which uses a float. Meaning you could do this

printf("rounded %f",roundf(change*100));

However you will end up with 420.000. What you are doing is multiplying change by 100 so 4.2x100 = 420 then you round that up and print it as a float so including 4 decimals.

I would suggest creating an int rounded_change and initializing it with the value of the rounded float or double, that way you can call it if you need it later.

Also I would recommend you take a look at the Style Guide because it will make your life and who ever looks at your codes life a lot easer in the future.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

0

The round function returns a double. Notice the function signature from the man page:

Synopsis

#include <math.h>

double round(double x);
float roundf(float x);
long double roundl(long double x);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .