module Network.Bitcoin.BitX.Types.Internal.Decimal
(
realToDecimalByteString_
)
where
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (pack)
import Numeric (showFFloat)
realToDecimalByteString_ :: (RealFrac a) => a -> ByteString
realToDecimalByteString_ k =
pack
. handleIntegers
. reverse . dropWhile (== '0') . reverse
$ (showFFloat Nothing . (fromRational :: Rational -> Double)
. toRational $ truncate6 k) ""
truncate6 :: RealFrac a => a -> Double
truncate6 k =
(/ (1000 * 1000 :: Double)) . fromInteger
$ truncate (k * 1000 * 1000)
handleIntegers :: String -> String
handleIntegers x =
if last x == '.'
then init x
else x