July 14th, 2003, 03:41 AM
Round up a decimal number in C++
How can I round a float variable which has a decimal number stored in it to 2 or any number of digits?
I tried looking functions in iomanip.h and math.h but couldn't find it. I also searched this forum but didnt get answer to my question.
Also I am working on a project that needs me to insert, the current date and time when the program is running, into my output file.
Is there any inbuilt function in C++ that can do it for me or am I supposed to create a class for that?
P.S. This mightbe helpful while answering the question.
Language = C++
Platform = WindowsXP
Environment = Visual Studio 6.0
July 14th, 2003, 07:36 AM
you can store digits in a double into an int just by assigning them. The dot and the numbers right to the dots will be loss.
double d = 1234.5678
int i = 0;
i = d;
July 14th, 2003, 10:46 AM
But that would truncate rather than round off.
What I've usually done to round off is (using the declarations above):
i = d + 0.5;
That way, if the number's fractional part is 0.5 or greater, the integer part has been incremented before the truncation. And if it was less than 0.5, then it will just get trunctated without affecting the integer part.
July 14th, 2003, 01:15 PM
July 14th, 2003, 06:05 PM
But watch out for negative numbers ;-)
July 17th, 2003, 04:13 AM
ooops i guess i didnt clear my point
the question was how to make
123.45678 = 123.46
that was the real question but i guess i made ppl think that it was like 123.45678 = 123
I would appreciate, If any one has a soln. for that.
July 17th, 2003, 11:10 AM
Basically, rescale, round off, then restore the original scale:
I broke that out to make each step more clear; you could probably write it as a one-line function. You should also be able to generalize it to whatever decimal place you'd want to round off to.
double RoundToHundredths(double n)
/* rescale 123.45678 to 12345.678 */
d = n * 100.0;
/* round off: 12345.678 + 0.5 = 12346.178 -> 12346 */
i = d + 0.5;
/* restore to its original scale: 12346 -> 123.46 */
d = (float)i / 100.0;
Also, there may well be other ways to do it and undoubtedly better.
Last edited by dwise1_aol; July 17th, 2003 at 11:13 AM.
July 17th, 2003, 01:03 PM
int RoundIt(double Num)
int co = 0;
while(Num > 10)
Num = ceil(Num);
for (int x = 0; x < co; x++)
Num *= 10;
ceil is a function that rounds UP to the next whole number. This fuction does not suit your needs, but could be easilly modified to do so. But the fuction takes the number down to a power of ten, ten later restores it by multiplying by that same power. (pretty much what dwise just did)
BTW iomanip does have setprecision
int main(int argc, char* argv)
double var = 123.12395;
cout << setprecision(2);
cout << var;
play around w/ it.
Last edited by AGibel; July 17th, 2003 at 01:21 PM.
July 17th, 2003, 01:16 PM
Hehe... you could just use:
It's in the iomanip library ( I think ). It 'stays' there though. But you can also use:
cout << setprecision(2) << whatever;
It does it just like that. But if you use the second option you have to put it before every number you are printing I think.
July 17th, 2003, 01:20 PM
man, thats so annoying when I forget to refresh the page and someone says what I just edited lol. Anyway, you could do that, and to keep it on all numbers just do:
cout << setiosflags(ios::fixed)
before the numbers.
July 18th, 2003, 02:03 AM
lol that s wot i was looking for "setprecision" i used it looooooong time ago but had forgot wot the function name was :P
Thanx Guys :D
July 18th, 2003, 01:15 PM
Doesn't anyone use the round() function?
using namespace std;
cout << round(x*10000)/10000.0;
June 22nd, 2004, 12:34 AM
Actually just check to see if the number is below 0, and if it is, subtract .5 instead of add .5, and it works just the same.
Originally Posted by BigBadBob
June 22nd, 2004, 01:14 AM
should of waited a few weeks and made it an even year
July 26th, 2004, 09:38 PM
The problem with round is that, at least on some compilers, it is discriminating the poor number zero so that it will not round a number under 0.5 to 0, but instead rounds it upwards to 1.
Originally Posted by dog135
One can overcome this by making a function like:
which also takes care of the problem with negative numbers.
int zero_round(double val)
However in this case, it's not good since it only rounds to integers, but if you encapsulate it in another function designed after your algorithm, you get:
which takes the value of choice, powers it by p (1=10, 2=100, 3=1000, etc.) and use the zero_round function to calculate the rounded float value with the precision p.
double float_round(double val, int p)
I find this combination rather usefull, at least for me.
I first tried stringstream because I like that class, being so multi-purpose, however I would hazard that the new one is a bit faster than letting stringstream take care of calculating it.
But if you would want to use the stringstream version, here it is:
Don't forget to include sstream and iomanip
double float_round(double val, int p)