Skip to content

Instantly share code, notes, and snippets.

@nullren
Created August 16, 2012 22:10
Show Gist options
  • Save nullren/3374057 to your computer and use it in GitHub Desktop.
Save nullren/3374057 to your computer and use it in GitHub Desktop.
need a better way to do "suffix"
humanReadable :: Double -> Double -> Integer -> String
humanReadable num base power
| num < 0 = "Negative file size!"
| power > 8 = "Fucking huge!"
| num > base = humanReadable (num / base) base (power + 1)
| otherwise = printf pstr num (suffix power)
where
pstr = if num > 10 then "%.0f%s" else "%.1f%s"
suffix 0 = ""
suffix 1 = "K" -- kilo
suffix 2 = "M" -- mega
suffix 3 = "G" -- giga
suffix 4 = "T" -- tera
suffix 5 = "P" -- peta
suffix 6 = "E" -- exa
suffix 7 = "Z" -- zetta
suffix 8 = "Y" -- yotta
suffix _ = "?"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment