Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C double value round up

Tags:

c

double

I have written a programm in C . Mostly, in the output is a point number. If I write "%.2lf" sometimes it round off but I would like that it allways round up. How can I do this?

like image 213
Bene Avatar asked Aug 26 '26 23:08

Bene


2 Answers

Since you obviously want to get 2 digits left behind the decimal dot you would need to transform the number before you useceil and then transform it back:

val = ceil( val * 100.0 ) / 100.0
printf( "%.2lf\n", val );
like image 83
x4u Avatar answered Aug 30 '26 07:08

x4u


Include math.h if you don't have it already, and do:

ceil(myDouble)
like image 43
Ry- Avatar answered Aug 30 '26 05:08

Ry-



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!