Skip to content

Instantly share code, notes, and snippets.

@mithrandi
Created January 11, 2021 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mithrandi/bddb422c9b751d104f85fd926d896a95 to your computer and use it in GitHub Desktop.
Save mithrandi/bddb422c9b751d104f85fd926d896a95 to your computer and use it in GitHub Desktop.
showRational :: Int64 -> Rational -> Builder
showRational len n = sign <> TS.showb d <> "." <> decs
where
sign =
if num < 0
then "-"
else mempty
decs = fromLazyText . TL.take len . toLazyText $ go next
(d, next) = abs num `quotRem` den
go 0 = mempty
go x =
let (d', next') = (10 * x) `quotRem` den
in TS.showb d' <> go next'
num = numerator n
den = denominator n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment