Im new to C++ and i'm not sure why the output for this code is 8 and not 8.25?
can someone explain why this code outputs an int not a double?
Thanks :)
#include "stdafx.h"
#include <iostream>
int main()
{
double x = 8.25;
int y;
y = x;
double z = static_cast<double>(y);
std::cout << z << std::endl;
return 0;
}
The data is converted to the integer 8
in the statement y = x
.
A static_cast
cannot recover the lost ".25" after throwing it away by converting to int
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.