Skip to content

Commit

Permalink
[ new ] utility for pretty printing Doubles
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-hoeck committed Apr 23, 2024
1 parent d07a748 commit f3774ca
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Text/Show/Pretty.idr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Text.Show.Pretty

import Data.List
import Data.List1
import Text.PrettyPrint.Bernardy

import public Text.Show.Value
Expand All @@ -13,6 +14,35 @@ import public Text.Show.PrettyVal.Derive
-- Utilities
--------------------------------------------------------------------------------

dropTrailingZeros : SnocList Char -> String
dropTrailingZeros [<] = "0"
dropTrailingZeros [<'.'] = "0"
dropTrailingZeros (i :< '.') = fastPack (i <>> [])
dropTrailingZeros (i :< '0') = dropTrailingZeros i
dropTrailingZeros sc = fastPack (sc <>> [])

roundUp : SnocList Char -> String
roundUp [<] = "1"
roundUp [<'.'] = "1"
roundUp (i :< '.') = show $ cast {to = Integer} (fastPack (i <>> [])) + 1
roundUp (i :< '9') = roundUp i
roundUp (i :< c) = fastPack (i <>> [chr $ ord c + 1])

||| Prints a floating point number with at most the given number
||| of digits after the dot.
export
printDouble : (precision : Nat) -> Double -> String
printDouble prec v =
case forget $ split ('.' ==) (fastUnpack $ show v) of
[x,y] =>
case splitAt prec y of
(pre,[]) => dropTrailingZeros ([<] <>< (x ++ '.' :: pre))
(pre,c::_) =>
if c <= '4'
then dropTrailingZeros ([<] <>< (x ++ '.' :: pre))
else roundUp ([<] <>< (x ++ '.' :: pre))
_ => show v

-- Don't put parenthesis around constructors in infix chains
isInfixAtom : Value -> Bool
isInfixAtom (InfixCons _ _) = False
Expand Down

0 comments on commit f3774ca

Please sign in to comment.