C/C++ counting the number of decimals?

后端 未结 13 1947
北荒
北荒 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 16:42

    Years after the fight but as I have made my own solution in three lines :

    string number = "543.014";    
    size_t dotFound;
    stoi(number, &dotFound));
    string(number).substr(dotFound).size()
    

    Of course you have to test before if it is really a float (With stof(number) == stoi(number) for example)

提交回复
热议问题