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

Daniel Fischer daniel.is.fischer at web.de
Wed Aug 26 05:46:28 EDT 2009


Am Mittwoch 26 August 2009 06:29:47 schrieb George Pollard:
> 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

Prelude> numDigits (10^15)
15




More information about the Haskell-Cafe mailing list