@@ -24,11 +24,10 @@ module Data.Text.Format
24
24
, shortest
25
25
) where
26
26
27
- #ifndef MIN_VERSION_double_conversion
28
- import Data.List (dropWhileEnd , isSuffixOf )
29
- import Numeric (showFFloat )
30
- #else
27
+ #ifdef MIN_VERSION_double_conversion
31
28
import Data.Double.Conversion.Text (toFixed , toShortest )
29
+ #else
30
+ import Numeric (showFFloat , showInt )
32
31
#endif
33
32
import qualified Formatting.Buildable as B
34
33
import Data.Text.Format.Types (Hex (.. ))
@@ -54,31 +53,31 @@ fixed :: (Real a) =>
54
53
Int
55
54
-- ^ Number of digits of precision after the decimal.
56
55
-> a -> Builder
57
- #ifndef MIN_VERSION_double_conversion
56
+ #ifdef MIN_VERSION_double_conversion
57
+ fixed decs = fromText . toFixed decs . realToFrac
58
+ #else
58
59
fixed decs = fromString . toFixed . realToFrac
59
60
where
60
61
toFixed :: Double -> String
61
62
toFixed dbl = showFFloat (Just decs) dbl " "
62
- #else
63
- fixed decs = fromText . toFixed decs . realToFrac
64
63
#endif
65
64
{-# NOINLINE [0] fixed #-}
66
65
67
66
-- | Render a floating point number using the smallest number of
68
67
-- digits that correctly represent it.
69
68
shortest :: Real a => a -> Builder
70
- #ifndef MIN_VERSION_double_conversion
69
+ #ifdef MIN_VERSION_double_conversion
70
+ shortest = fromText . toShortest . realToFrac
71
+ #else
71
72
shortest = fromString . toShortest . realToFrac
72
73
where
73
74
toShortest :: Double -> String
74
- toShortest dbl = do
75
- let shownFullPrec = showFFloat Nothing dbl " "
76
- strip = dropWhileEnd (== ' 0' ) shownFullPrec
77
- if " ." `isSuffixOf` strip
78
- then take (length strip - 1 ) strip
79
- else strip
80
- #else
81
- shortest = fromText . toShortest . realToFrac
75
+ toShortest dbl =
76
+ -- `showFFloat (Just 0) "" 1.0` gives "1.", but we want "1"
77
+ let intPart = (floor dbl :: Int ) in
78
+ if dbl == (fromIntegral intPart)
79
+ then showInt intPart " "
80
+ else showFFloat Nothing dbl " "
82
81
#endif
83
82
{-# INLINE shortest #-}
84
83
0 commit comments