Created
August 16, 2012 22:10
-
-
Save nullren/3374057 to your computer and use it in GitHub Desktop.
need a better way to do "suffix"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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