DEV Community

Cover image for C# Int.ToString().Length in C
Adam K Dean
Adam K Dean

Posted on

C# Int.ToString().Length in C

#c

Another little helper function here, it counts how long the int would be if it was a string, just the same as doing Int.ToString().Length in C#.

/*
 * File:   int_strlen.c
 * Author: Adam K Dean
 * Description: get length of an int as if it was a string
 *
 * Created on 11 January 2011, 13:01
 */

int int_strlen(int val)
{
    int v = val / 10;
    int i = 1, count = 1;

    while(v > i - 1)
    {
        i *= 10;
        count ++;
    }

    return count;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)