pub enum RoundingStrategy {
MidpointAwayFromZero,
MidpointNearestEven,
ToZero,
AwayFromZero,
ToNegativeInfinity,
ToPositiveInfinity,
}Expand description
How a value that does not land on a step is resolved.
The Midpoint variants round to the nearest step and differ only in how
they settle an exact tie. The rest are directed: they resolve every inexact
value the same way, however close it sits to either side.
Dec::round_dp and Dec::round_to_step round
MidpointAwayFromZero, which is
also the Default, so a policy stored per instrument starts where the
plain methods do.
use troy::{RoundingStrategy, dec};
let price = dec!(104_237.286);
assert_eq!(price.round_dp_with(2, RoundingStrategy::ToZero), dec!(104_237.28));
assert_eq!(price.round_dp_with(2, RoundingStrategy::ToPositiveInfinity), dec!(104_237.29));Variants§
MidpointAwayFromZero
To the nearest step, a tie away from zero. The default, and what a price coming out of a model wants when it is pinned to a tick.
MidpointNearestEven
To the nearest step, a tie to the even step. Banker’s rounding: use it where many rounded values are summed, such as settlement or an accumulating PnL, and the away-from-zero bias would compound.
ToZero
Towards zero, discarding the rest. What an exchange quantity filter does to a size, and so the safe direction for an order that must not exceed a balance.
AwayFromZero
Away from zero, always up in magnitude. For a margin, a fee or a buffer that has to cover the exact value.
ToNegativeInfinity
Down, towards negative infinity. Floor: the bid-side price rounding, so a quote never creeps up into the spread.
ToPositiveInfinity
Up, towards positive infinity. Ceil: the ask-side counterpart.