Page 1 of 1

Round up float value

Posted: Sun Sep 08, 2002 6:20 pm
by rakeb
is there any function in C to round up float value i.e we can write
4.236 as 4.24 or I have to write it?

Posted: Sun Sep 08, 2002 6:36 pm
by fpnc
I'm not a guru of C, but maybe something like ((int)(4.236*100))/100.0 ?

Posted: Sun Sep 08, 2002 7:06 pm
by Ivan Golubev
sprintf/printf("%.2lf", x);

Posted: Sun Sep 08, 2002 7:32 pm
by rakeb
i don't want print the round up value i want to assign it in a variable
i.e.
[c]
if(a==4.346)
a=round(4.346); //a will be 4.35

[/c]
is there any way.....

Posted: Sun Sep 08, 2002 11:19 pm
by Adrian Kuegel
If you want to round the number to nearest integer, use
a = floor(a*100+0.5)/100.0;
and if you want to round to next higher integer, use
a = ceil(a*100)/100.0;

Posted: Tue Sep 10, 2002 5:28 am
by imranul

Code: Select all

char str[100];
sprintf(str,"%.2f",a);
a=atof(str);
// be sure u're not using Turbo C++.The atof function in it has bugs.
// u can write a subroutine also that does the same thing life atof.
Imran