[Haskell-cafe] How to calculate de number of digits of an integer? (was: Is logBase right?)

George Pollard porges at porg.es
Wed Aug 26 00:29:47 EDT 2009


You could also fudge the input:

{-# LANGUAGE NoMonomorphismRestriction #-}

log10 = floor . logBase 10 . (0.5+) . fromIntegral

numDigits n | n < 0 = 1 + numDigits (-n)
numDigits 0         = 1
numDigits n         = 1 + log10 n

-- checked [0..10^8], finding a counter-example is left as an exercise :P


More information about the Haskell-Cafe mailing list