Integer 类里计算数字是几位数字的方法

来源:互联网 发布:浙江大学软件学院地址 编辑:程序博客网 时间:2024/06/06 20:22
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,99999999, 999999999, Integer.MAX_VALUE };

    // Requires positive x
    static int stringSize(int x) {
        for (int i=0; ; i++)
            if (x <= sizeTable[i])
                return i+1;
    }
0 0
原创粉丝点击