diff options
author | Andrew Cady <d@cryptonomic.net> | 2022-09-15 22:58:30 -0400 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2022-09-15 23:03:09 -0400 |
commit | fab434598d0958c9cdc339822017748cc5bd2461 (patch) | |
tree | 80b32636de3a1583e33da5e2cc8bab60a59938bd | |
parent | c70ec8a408400291abc83a701539db946444a053 (diff) |
large number formatting
-rwxr-xr-x | countdown.hs | 15 | ||||
-rw-r--r-- | package.yaml | 1 |
2 files changed, 14 insertions, 2 deletions
diff --git a/countdown.hs b/countdown.hs index 3f39a7d..b53a3a4 100755 --- a/countdown.hs +++ b/countdown.hs | |||
@@ -22,6 +22,7 @@ import Control.Monad.RWS | |||
22 | import Data.Time.Calendar.OrdinalDate | 22 | import Data.Time.Calendar.OrdinalDate |
23 | import Data.Text.Format.Numbers | 23 | import Data.Text.Format.Numbers |
24 | 24 | ||
25 | import Data.Text (unpack) | ||
25 | import Control.Lens | 26 | import Control.Lens |
26 | import Control.Monad (void, forever) | 27 | import Control.Monad (void, forever) |
27 | import Control.Concurrent (threadDelay, forkIO) | 28 | import Control.Concurrent (threadDelay, forkIO) |
@@ -172,6 +173,15 @@ cosmicConversion = borderWithLabel (str "Cosmic Conversion") $ renderTable $ | |||
172 | [str "100 years" , showCosmicYears 100] | 173 | [str "100 years" , showCosmicYears 100] |
173 | ] | 174 | ] |
174 | 175 | ||
176 | showLarge :: Double -> String | ||
177 | showLarge n | n >= 10 * 1000 * 1000 * 1000 = printf "%.0f billion" (n / (1000 * 1000 * 1000)) | ||
178 | showLarge n | n >= 1000 * 1000 * 1000 = printf "%.1f billion" (n / (1000 * 1000 * 1000)) | ||
179 | showLarge n | n >= 10 * 1000 * 1000 = printf "%.0f million" (n / (1000 * 1000)) | ||
180 | showLarge n | n >= 1000 * 1000 = printf "%.1f million" (n / (1000 * 1000)) | ||
181 | showLarge n | n >= 10 * 1000 = printf "%.0f thousand" (n / 1000) | ||
182 | showLarge n | n >= 10 = unpack $ commasF 0 n | ||
183 | showLarge n = printf "%.3f" n | ||
184 | |||
175 | countdownWidget :: Bool -> LocalTime -> Widget n | 185 | countdownWidget :: Bool -> LocalTime -> Widget n |
176 | countdownWidget isSimulated (truncateTime -> t) = | 186 | countdownWidget isSimulated (truncateTime -> t) = |
177 | (hCenter (borderWithLabel (str $ printf "Current time%s" (if isSimulated then " (SIMULATED)" else "")) $ | 187 | (hCenter (borderWithLabel (str $ printf "Current time%s" (if isSimulated then " (SIMULATED)" else "")) $ |
@@ -194,13 +204,14 @@ countdownWidget isSimulated (truncateTime -> t) = | |||
194 | (commas secondsLeft) | 204 | (commas secondsLeft) |
195 | (pluralize secondsLeft) | 205 | (pluralize secondsLeft) |
196 | (pluralizeVerb secondsLeft))) | 206 | (pluralizeVerb secondsLeft))) |
197 | , (borderWithLabel (str "Cosmic Countdown") $ | 207 | , (borderWithLabel (str "Cosmic Time") $ |
198 | vBox [ (str $ printf "%s years ago" (commas $ (floor cosmicYearsAgo :: Integer))) | 208 | vBox [ (str $ printf "%s years ago" (commas $ (floor cosmicYearsAgo :: Integer))) |
199 | , (str "\n\n") | 209 | , (str "\n\n") |
210 | , (str $ printf "%s years ago" (showLarge $ (realToFrac cosmicYearsAgo))) | ||
200 | -- , (str $ printf "%s billion years ago" (commasF 9 $ cosmicYearsAgo / (1000*1000*1000))) | 211 | -- , (str $ printf "%s billion years ago" (commasF 9 $ cosmicYearsAgo / (1000*1000*1000))) |
201 | -- , (str $ printf "%s million years ago" (commasF 6 $ cosmicYearsAgo / (1000*1000))) | 212 | -- , (str $ printf "%s million years ago" (commasF 6 $ cosmicYearsAgo / (1000*1000))) |
202 | -- , (str $ printf "%s thousand years ago" (commasF 3 $ cosmicYearsAgo / 1000)) | 213 | -- , (str $ printf "%s thousand years ago" (commasF 3 $ cosmicYearsAgo / 1000)) |
203 | , (str $ printf "%s days ago" (commas $ (floor $ realToFrac cosmicYearsAgo * daysPerYear :: Integer))) | 214 | -- , (str $ printf "%s days ago" (commas $ (floor $ realToFrac cosmicYearsAgo * daysPerYear :: Integer))) |
204 | ]) | 215 | ]) |
205 | ]) | 216 | ]) |
206 | , | 217 | , |
diff --git a/package.yaml b/package.yaml index fa26a44..d081cde 100644 --- a/package.yaml +++ b/package.yaml | |||
@@ -12,6 +12,7 @@ dependencies: | |||
12 | - containers | 12 | - containers |
13 | - brick | 13 | - brick |
14 | - format-numbers | 14 | - format-numbers |
15 | - text | ||
15 | 16 | ||
16 | executables: | 17 | executables: |
17 | countdown: | 18 | countdown: |