Round up float value

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
rakeb
New poster
Posts: 42
Joined: Fri Aug 30, 2002 2:51 pm
Location: France

Round up float value

Post 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?

fpnc
System administrator
Posts: 201
Joined: Sun Oct 07, 2001 2:00 am
Location: Valladolid, Spain

Post by fpnc »

I'm not a guru of C, but maybe something like ((int)(4.236*100))/100.0 ?
Best regards,

Fernando N

Ivan Golubev
Experienced poster
Posts: 167
Joined: Fri Oct 19, 2001 2:00 am
Location: Saint Petersburg, Russia

Post by Ivan Golubev »

sprintf/printf("%.2lf", x);

rakeb
New poster
Posts: 42
Joined: Fri Aug 30, 2002 2:51 pm
Location: France

Post 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.....

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post 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;

imranul
New poster
Posts: 12
Joined: Fri Jul 19, 2002 6:28 pm
Location: Bangladesh

Post 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
Life is like a box of Chocolates,
you never know what you're going to get...

Post Reply

Return to “C”