Method only for math:
function leftMost($num) { return floor($num/pow(10,(floor((log10($num)))))); }
explained, I think ...
1+ log10 from the number calculates the number of digits whose number we use, we use it to remove any decimal values, put it as an exponent, so for a 1-digit number we get 10 ^ 0 = 1 or an 8-digit number we get 10 ^ 8 Then we simply divide 12345678/10000000 = 1.2345678, which gets floor'd and is only 1.
note: this works for numbers between zero and one as well, where it will return 2 to 0.02 and the string conversion will fail.
If you want to work with negative numbers, first do $ num = abs ($ num).
Incognito
source share