Search:
Forum
General C++ Programming
devision
devision
Nov 5, 2009 at 10:30am UTC
mar11
(50)
Hi all,
assuming this code:
1
2
3
4
5
6
int
a; a = 90 / 20; cout <<
"a"
<<a<<endl;
// output is 4
How can i declare the variable if i want :
1- to get 90 /20 = 4.5
2- to get 90/20 = 5
thanks for each advance
Nov 5, 2009 at 10:36am UTC
bluezor
(298)
You can declare it as
double
a;
for it to be able to take decimal points.
You can round up / round down a value using <cmath>'s two functions -
* ceil -
http://www.cplusplus.com/reference/clibrary/cmath/ceil/
(rounding up)
* floor -
http://www.cplusplus.com/reference/clibrary/cmath/floor/
(rounding down)
Nov 5, 2009 at 11:05am UTC
mar11
(50)
@bluezor: thanks a lot
Nov 5, 2009 at 4:19pm UTC
firedraco
(6247)
Also, you need to change either 90 or 20 to a float/double (otherwise you will get 4 because of integer division).
Topic archived. No new replies allowed.
C++
Information
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs