Skip to content

Instantly share code, notes, and snippets.

@monomadic
Last active October 7, 2018 08:52
Show Gist options
  • Save monomadic/8f695095da519d7961e4faf97e160cdf to your computer and use it in GitHub Desktop.
Save monomadic/8f695095da519d7961e4faf97e160cdf to your computer and use it in GitHub Desktop.
Convert a float to an int with JSON.Decode
import Json.Decode as Decode
type alias CoinPair = { pair: String, price: Float, rsi_15m: Int }
decodeCoinPair : Decode.Decoder CoinPair
decodeCoinPair =
Decode.map5 CoinPair
(Decode.field "pair" Decode.string)
(Decode.field "price" Decode.float)
(Decode.field "rsi_15m" decodeFloatAsInt)
decodeFloatAsInt : Decode.Decoder Int
decodeFloatAsInt =
Decode.map
round(Decode.oneOf [Decode.float, Decode.null 0.0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment