推断出多个位置的数量

时间:2012-10-25 15:53:14

标签: java algorithm

我想提取一个简单数字的位置数: 示例:123位于3位置,12位于2位置,12365位于5位置...... 什么是解决方案? Math中有一种方法吗?

2 个答案:

答案 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()将数字向下舍入以获得位数。