What is a way of getting the order of magnitude of an integer (or float) in a solidity contract?
e.g. such that
MyContract.getOrderMag(2564) returns 3
MyContract.getOrderMag(987) returns 2
MyContract.getOrderMag(2.3e76) returns 76
Ideally in the least gas using way possible...
Here's what I have:
pragma solidity ^0.4.6;
contract Magger {
function getOrderMag(int256 input) constant returns (int256){
int counter=0;
int temp = input;
while((temp/10)>1){
temp = temp/10;
counter++;
}
return counter;
}
}
This doesn't work with negative numbers or, as pointed out by @RichardHorrocks, when input is 10.