paritytech / primitives

Other
33 stars 23 forks source link

Best way to handle floats/decimals? #59

Open BlinkyStitt opened 6 years ago

BlinkyStitt commented 6 years ago

How can I get a float/decimal type out of dividing 2 U256s?

I have an order object from a 0x relay with these values that are too big for a f64 and I want to display the price.

{
    "makerAssetAmount": "386028708828325200000000",
    "takerAssetAmount": "2999999999999999875",
    ...
}

With integer division on a U256, 2999999999999999875/386028708828325200000000 = 0, but I want 0.000007771442723. I know using floats with currency can cause problems with rounding, but I think the way I am trying to use it for display is okay. If not, please point me to a better way.

BlinkyStitt commented 6 years ago

For now, I'm using https://crates.io/crates/bigdecimal instead of U256 for these types.