### Thread: Round up a decimal number in C++

Page 1 of 2 12 Last
1. #### 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?

Thanxxx...

Language = C++
Platform = WindowsXP
Environment = Visual Studio 6.0
2. 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;
3. Originally posted by kubicon
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;
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.
4. Nice!
5. No Profile Picture
status unknown
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Posts
262
Rep Power
12
Originally posted by dwise1_aol
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.
But watch out for negative numbers ;-)
6. 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.
ThnQ
7. Originally posted by mr_______bond
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.
ThnQ
Basically, rescale, round off, then restore the original scale:
Code:
```double RoundToHundredths(double n)
{
double d;
int    i;

/* 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;

return d;
}```
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.

Also, there may well be other ways to do it and undoubtedly better.
Last edited by dwise1_aol; July 17th, 2003 at 10:13 AM.
8. int RoundIt(double Num)
{
int co = 0;

while(Num > 10)
{
Num/= 10;
co++;
}

Num = ceil(Num);
for (int x = 0; x < co; x++)
Num *= 10;

return Num;
}

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

#pragma hdrstop
#include <iomanip.h>
#include <conio.h>
#include <iostream.h>

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
double var = 123.12395;

cout << setprecision(2);
cout << var;
getch();
return 0;
}

play around w/ it.
Last edited by AGibel; July 17th, 2003 at 12:21 PM.
9. Hehe... you could just use:
cout.precision(2);
whatever number.

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.
10. 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.
11. 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
12. Doesn't anyone use the round() function?

This works:
Code:
```#include <iostream>
using namespace std;

void main()
{
float x;
x=3.1415927;
cout << round(x*10000)/10000.0;
}```
Output: 3.1416
13. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2004
Posts
1
Rep Power
0
But watch out for negative numbers ;-)
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.
14. should of waited a few weeks and made it an even year
15. No Profile Picture
ion
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2004
Posts
5
Rep Power
0
Originally Posted by dog135
Doesn't anyone use the round() function?

This works:
Code:
```#include <iostream>
using namespace std;

void main()
{
float x;
x=3.1415927;
cout << round(x*10000)/10000.0;
}```
Output: 3.1416
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.

One can overcome this by making a function like:
Code:
```int zero_round(double val)
{
return (int)(trunc(val+(val>=0.0?0.5:-0.5)));
}```
which also takes care of the problem with negative numbers.

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:
Code:
```double float_round(double val, int p)
{
double t=pow(10.0,p);
return (double)zero_round(val*t)/t;
}```
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.

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:
Code:
```double float_round(double val, int p)
{
stringstream s;
s<<setprecision(p)<<setiosflags(ios_base::fixed)<<val;
s>>val;
return val;
}```
Don't forget to include sstream and iomanip
Page 1 of 2 12 Last