Home > database >  Need help rounding a double number in printf
Need help rounding a double number in printf

Time:04-06

I'am trying to round a number of a multiply sum but I have no luck with it.

I think I'am using the round function wrong. But I can't find anything.

int main()
{
    double euro, ats, sum;

    euro = 414;

    ats = 13.760;
    // Umrechnen in Komma
    sum = euro * ats;
    printf("Summe = %f\n", sum, lround(sum));
    return 0;
}

My Goal is that the calculation 414 * 13.760 = 5697 rounded.

CodePudding user response:

Your code is passing 2 arguments to printf and your format expects only 1 (%f).

printf("Summe = %f\n", sum, lround(sum));

So you could simply (as Eugene said in the comments):

printf("Summe = %.0f\n", sum);

In this case you're printing a float rounded to digits after the decimal point (.0).


If you really want to use lround, notice that it returns a long (some ref). So, you'd better:

printf("Summe = %ld\n", lround(sum));

Or check the math.h round, ceil or floor functions too!

  • Related