Is there a way to check if a variable is a whole number? C++

前端 未结 7 717
北恋
北恋 2020-12-20 14:01

I need to check if a variable is a whole number, say I have the code:

double foobar = 3;
//Pseudocode
if (foobar == whole)
    cout << \"It\'s whole\";         


        
相关标签:
7条回答
  • 2020-12-20 14:45

    Assuming foobar is in fact a floating point value, you could round it and compare that to the number itself:

    if (floor(foobar) == foobar)
        cout << "It's whole";
    else
        cout << "Not whole";
    
    0 讨论(0)
提交回复
热议问题