Get number of digits of a number

前端 未结 9 1083
挽巷
挽巷 2021-01-07 11:39

I have a number like this: int num = 36729; and I want to get the number of digits that compose the number (in this case 5 digits).

How can I do this?

9条回答
  •  终归单人心
    2021-01-07 12:18

    Inefficient, but strangely elegant...

    #include 
    #include 
    
    int main(void)
    {
        // code to get value
        char str[50];
        sprintf(str, "%d", value);
    
        printf("The %d has %d digits.\n", value, strlen(str));
    
        return 0;
    }
    

提交回复
热议问题