C/C++ int运算结果转float/double 保留小数

#include <iostream>
using namespace std;

int main()
{
   cout << "Hello World\n";
	int x = 50;
    int y = 12;
    
    cout << x / y << endl;
    
    cout << (float) (x / y) << endl;
    
    cout << (double) (x / y) << endl;
    
    cout << (double) x / (double) y << endl;
    
    return 0;
}

C/C++中int类型变量运算结果转float/double探究 - MK_筱雨 - 博客园在编程中,经常用到的一个结果转换就是两个int类型变量相除的结果为了保留精确度而转换为float或者double类型,但是这个地方最容易令人犯错误的是,如果使用如下的类型转换方式,转换时是将两个inthttps://www.cnblogs.com/MK-XIAOYU/p/12466711.html 

猜你喜欢

转载自blog.csdn.net/u013288190/article/details/127119183