C/C++ counting the number of decimals?

后端 未结 13 1942
北荒
北荒 2020-12-06 16:04

Lets say that input from the user is a decimal number, ex. 5.2155 (having 4 decimal digits). It can be stored freely (int,double) etc.

Is there any

相关标签:
13条回答
  • 2020-12-06 17:01

    Something like this might work as well:

    float i = 5.2154;
    std::string s;
    std::string t;
    std::stringstream out;
    out << i;
    s = out.str();
    
    t = s.substr(s.find(".")+1);
    cout<<"number of decimal places: " << t.length();
    
    0 讨论(0)
提交回复
热议问题