How do I make it so that this statement shows .8 instead of 0:
cout << 4/5;
It keeps displaying 0 instead of .8. Thanks for your help.
How do I make it so that this statement shows .8 instead of 0:
cout << 4/5;
It keeps displaying 0 instead of .8. Thanks for your help.
cout<<4.0/5.0;
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
Now how would I do that if it were a variable?
If it were a variable, the << operator would know how. If both top/bottom are floats, it will output 0.8. But if they are ints, then you will get the result of integer division, which drops the remainder and the << will output an int, or 0.
Type cast it. I believe C++ likes you to use "static_cast". IIRC, it's something like:
Quzah.Code:int var1 = 4, var2 = 5; cout << static_cast<float>(var1) / static_cast<float>(var2);
Hope is the first step on the road to disappointment.
Here is my line of code:
Both of the variables, c & n, were defined as integers. I still can't get the line to print out any decimals. I'm going to fiddle around with it, but maybe you know what I am doing wrong? Thanks for all your help already quzah.cout << "PI= " << 4 * static_cast<float>(c) / static_cast<float>(n) << endl;
Well, if you look at that line, you'll see that 4 isn't a floating point number. Cast it also, or just ad a .0 to it.
Quzah.
Hope is the first step on the road to disappointment.
Thanks for your help, it worked!
Even casting and performing a floating point division operation might result in no decimals being output if the result of that operation results in a value such as X.0. The decimals will not be shown if there isn't anything to show or unless you tell cout to format its output differently:Originally Posted by scrub05
My output:Code:#include <iostream> #include <iomanip> using namespace std; int main() { float f = 5.0f; cout << "f is: " << f << endl; cout << setiosflags(ios::fixed) << setprecision(4) << "f is: " << f << endl; int c = 8, n = 4; float result = 4 * static_cast<float>(c) / static_cast<float>(n); cout.unsetf(ios::fixed); cout << "Result is: " << result << endl; cout << "Result is: " << setiosflags(ios::fixed) << setprecision(2) << result << endl; return 0; }
So you see it can work but depending on what your values for c and n are you might not be seeing the decimals.Code:f is: 5 f is: 5.0000 Result is: 8 Result is: 8.00
Last edited by hk_mp5kpdw; 01-06-2005 at 04:54 PM.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
I'd sooner write a function which prototypes the arguments as Double, as well as returns a Double (regardless what type you passed to it). Then allow C++'s Promotion Rules to do the work for you. Much more elegant IMHO. Here's a sample proggie...
Code:#include <iostream> using namespace std; double divide ( double, double ); int main() { int x, y; x = 4; y = 5; cout << "4 / 5 = " << divide ( x, y ); while ( cin.get() != '\n' ); return 0; } double divide ( double a, double b ) { return a / b; }
Last edited by Scribbler; 01-06-2005 at 06:53 PM.
I woudl just use a solution like what XSquared Said; define your int as a float,
ie- Intstead of INT X call it Float X. That should get you squared away.