为什么这个输出是0%?

时间:2017-01-15 03:27:53

标签: c++ types

该程序将计算比率。正确的输出应为4.50492%,但我的输出为0%。我怀疑这是尺寸问题。但是,double的大小是8个字节,因此类型long long是。我的计划有什么问题?谢谢。

#include <iostream>

int main()
{
    using namespace std;
    //cout << "Enter the world's population: ";
    long long world = 6898758899;
    //cin >> world;
    //cout << "Enter the population of US: ";
    long long us = 310783781;
    //cin >> us;

    double ratio = us/world * 100;
    char percentage = '%';
    cout << "The population of the US is " << ratio << percentage << " of the world population." << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:1)

因为&#34;我们&#34;和#34;世界&#34;类型很长很长。所以&#34;我们/世界&#34;很长。(长)0.0450492 == 0。

正确的拼写是

double ratio = (double)us/(double)world * 100.0;