Skip to content

Commit c351d74

Browse files
Fix crash on negative integers
Resolves #85
1 parent c9809b3 commit c351d74

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/Data/Text/Format.hs

+3-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ shortest = fromString . toShortest . realToFrac
7373
where
7474
toShortest :: Double -> String
7575
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 ""
76+
let intPart = fromIntegral (floor dbl :: Int) in
77+
if dbl == intPart
78+
then showFFloat (Just 0) intPart ""
8079
else showFFloat Nothing dbl ""
8180
#endif
8281
{-# INLINE shortest #-}

test/Spec.hs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ spec = do
4949
it "Fixed" $ format (fixed 4) (12.123456 :: Double) `shouldBe` "12.1235"
5050
it "Variable" $ format float (12.123456 :: Double) `shouldBe` "12.123456"
5151
it "Shortest" $ format shortest (12.0000 :: Double) `shouldBe` "12"
52+
it "Negative" $ format float (-1.0000 :: Double) `shouldBe` "-1"
5253

5354
describe "Scientific" $ do
5455
it "sci" $ format sci (scientific 60221409 16) `shouldBe` "6.0221409e23"

0 commit comments

Comments
 (0)