Skip to content

Commit

Permalink
Fix crash on negative integers
Browse files Browse the repository at this point in the history
Resolves AJChapman#85
  • Loading branch information
spencerjanssen committed Mar 5, 2024
1 parent c9809b3 commit c351d74
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/Data/Text/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ shortest = fromString . toShortest . realToFrac
where
toShortest :: Double -> String
toShortest dbl =
-- `showFFloat (Just 0) "" 1.0` gives "1.", but we want "1"
let intPart = (floor dbl :: Int) in
if dbl == (fromIntegral intPart)
then showInt intPart ""
let intPart = fromIntegral (floor dbl :: Int) in
if dbl == intPart
then showFFloat (Just 0) intPart ""
else showFFloat Nothing dbl ""
#endif
{-# INLINE shortest #-}
Expand Down
1 change: 1 addition & 0 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ spec = do
it "Fixed" $ format (fixed 4) (12.123456 :: Double) `shouldBe` "12.1235"
it "Variable" $ format float (12.123456 :: Double) `shouldBe` "12.123456"
it "Shortest" $ format shortest (12.0000 :: Double) `shouldBe` "12"
it "Negative" $ format float (-1.0000 :: Double) `shouldBe` "-1"

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

0 comments on commit c351d74

Please sign in to comment.