OliverNChalk / const-decimal

Integer-backed decimals with constant precision
Apache License 2.0
1 stars 1 forks source link

Add `quantize_toward_zero` method to `Decimal` #13

Closed MathisWellmann closed 1 month ago

MathisWellmann commented 1 month ago

This PR adds the ability to quantize a Decimal to a multiple of a quantum (rounding towards zero as its the default rounding behaviour in rust). The name is chosen to maximize the information gained from reading it (as compared with just naming it quantize and expecting a different rounding behaviour).

The use cases are plenty with one example being the ability to pass exchange PriceFilter and QuantityFilter, e.g when you have a price estimate from some signal processing function like a moving average (312.95137, stored in Decimal<i64, 5>) and the exchange expects you to be quantized to the tick size of 0.5 units of the quote currency, then this method comes in handy and calling .quantize_toward_zero(Decimal::try_from_scaled(5, 1).unwrap()) will return a Decimal of value 312.5.

In principle other such methods could exist for different rounding modes, e.g quantize_round, quantize_ceil, quantize_floor but those are out of scope of this PR.