From b1dbc88d9efd410ca8003fa086c1a0dd41ac23e3 Mon Sep 17 00:00:00 2001 From: Denis Tereshkin Date: Thu, 18 Jul 2019 20:33:30 +0700 Subject: [PATCH] Price: fix fromDouble conversion --- src/ATrade/Price.hs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ATrade/Price.hs b/src/ATrade/Price.hs index 023eda3..722a556 100644 --- a/src/ATrade/Price.hs +++ b/src/ATrade/Price.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE MultiWayIf #-} +{-# LANGUAGE MultiWayIf #-} {-# LANGUAGE OverloadedStrings #-} module ATrade.Price ( @@ -11,13 +11,13 @@ module ATrade.Price ( toScientific ) where -import Data.Int -import Data.Ratio +import Data.Int +import Data.Ratio -import Data.Aeson -import Data.Scientific +import Data.Aeson +import Data.Scientific -import Text.Printf +import Text.Printf data Price = Price { priceQuants :: !Int64 @@ -37,7 +37,7 @@ instance Num Price where signum a = a { priceQuants = signum (priceQuants a)} - fromInteger int = Price { priceQuants = mega * fromInteger int} + fromInteger int = Price { priceQuants = mega * fromInteger int} negate a = a { priceQuants = negate (priceQuants a) } @@ -45,7 +45,7 @@ toDouble :: Price -> Double toDouble p = fromIntegral (priceQuants p) / fromIntegral mega fromDouble :: Double -> Price -fromDouble d = Price { priceQuants = truncate (d * fromIntegral mega) } +fromDouble d = Price { priceQuants = truncate ((d * fromIntegral mega) + 0.5) } toScientific :: Price -> Scientific toScientific p = normalize $ scientific (toInteger $ priceQuants p) (-6)