我想提取一个简单数字的位置数:
示例:123
位于3
位置,12
位于2
位置,12365
位于5
位置......
什么是解决方案? Math
中有一种方法吗?
答案 0 :(得分:7)
您可以使用字符串:
int numberOfFigures = String.valueOf(12345).length();
或采取log10 of the number并将其四舍五入:
int numberOfFigures = Math.floor(Math.log10(12345)) + 1;
答案 1 :(得分:6)
执行此操作的有效方法是:Math.floor(Math.log10(x) + 1)
需要+1
来处理10的完美幂的情况。实际上Math.log10(10)
是1,但这是2位数。然后Math.floor()
将数字向下舍入以获得位数。