c++ - explicit type casting float(intvar) results in int -
i tried following code :
#include <iostream> #include<conio.h> using namespace std; int main() { int intvar = 25; float floatvar = 35.87; cout << "intvar= " << intvar; cout << "\n floatvar =" << floatvar; cout << "\n float(intvar)=" << float(intvar); cout << "\n int(floatvar)=" << int(floatvar); _getch(); return 0; } the result float(intvar) coming 25.
can please explain why still being shown integer , not 25.000000?
the value not integer, it's std::cout try give compact representation here. use setfixed , setprecision (from #include <iomanip>) force specific output precision on floats. add before first cout line:
cout << fixed << setprecision(6);
Comments
Post a Comment