Numbers after decimal point

Mar 14, 2011 at 5:20pm
So lets say i try to divide 10 by 3 (10 / 3) and the answer should be 3.33333(3)
how can i make the code to get me 4 numbers after decimal (3.3333)?
and tell me what i have to include :).

i got this:

int square;
square = ((b * b) - (4 * a * c));

Thanks !

EDIT: sorry, did not see beginner's forum :S.. i am a beginner
Last edited on Mar 14, 2011 at 5:21pm
Mar 14, 2011 at 5:23pm
Integers can only store... well, integer numbers. If you want the post decimal point digits you will need to floats/ doubles.
Mar 14, 2011 at 5:28pm
can you like me more specific?

i tryed this

square = float((b * b) - (4 * a * c));

but it have me the same answer :S
Mar 14, 2011 at 5:30pm
You got to make "square" a double, so it can store decimals. Then, if you want it to display an excact number of decimals, use "setprecision()". For example:

1
2
double square = 10/3;
cout << setprecision(4) << square
Last edited on Mar 14, 2011 at 5:30pm
Mar 14, 2011 at 5:36pm
Thank you! That was what i needed !
Mar 14, 2011 at 5:38pm
10/3 will still be an integer division because 3 and 10 are both integers.
You need to use a double literal to make it a double: 10.0 / 3; ( the 3 will be promoted to a double automatically )
Mar 14, 2011 at 5:58pm
i made exactly as Stupebrett said, but used Float instead of double
Topic archived. No new replies allowed.