How can I count the digits in an integer without a string cast?

前端 未结 10 1143
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 00:54

I fear there\'s a simple and obvious answer to this question. I need to determine how many digits wide a count of items is, so that I can pad each item number with the m

10条回答
  •  攒了一身酷
    2020-12-03 01:43

    You can loop through and delete by 10, count the number of times you loop;

    int num = 423;
    int minimum = 1;
    while (num > 10) {
        num = num/10;
        minimum++;
    }
    

提交回复
热议问题