How to get length of integers in PHP?

Posted by – May 25, 2016

在寫 code 的時候想到這個無關緊要的問題... 如何得知一個數字有幾位數?


function digits($num) {
    return (int) strlen((string) abs($num));

在 stackoverflow 看到一個也不賴的寫法....

function digits($num) {
    return (int) log(abs($num), 10) + 1;

筆記一下,都快忘了 log 怎麼算 XD

Leave a Reply

Your email address will not be published. Required fields are marked *