C doubled value rounded

I wrote a program in C. Basically, the output is a point number. If I write "% .2lf", sometimes it ends, but I would like it to always be rounded. How can i do this?

+3
source share
3 answers

Since you obviously want to get 2 digits left after the decimal point, you will need to convert the number before using ceil, and then translate it back:

val = ceil( val * 100.0 ) / 100.0
printf( "%.2lf\n", val );
+7
source

Turn it on math.hif you don’t have it yet and do:

ceil(myDouble)
+1
source

Use the function ceilfrom math.h.

0
source

All Articles