Skip to main content

dec

Macro dec 

Source
macro_rules! dec {
    ($($token:tt)+) => { ... };
}
Expand description

Builds a Dec from a decimal literal at compile time.

The tokens are stringified and handed to Dec::parse_const inside a const block, so the value is a constant and a malformed literal fails the build instead of the trade. The literal accepts everything FromStr does: a sign, an exponent, _ separators, and more than Dec::SCALE decimal places, where the excess rounds half away from zero.

use troy::{Dec, dec};

const TICK: Dec = dec!(0.01);
assert_eq!(dec!(104_237.25) + TICK, dec!(104_237.26));
assert_eq!(dec!(-1.5e3), dec!(-1500));
assert_eq!(dec!(0.0000000000000000005), Dec::EPSILON);

A literal that does not parse, or one outside the finite range, is rejected by the compiler:

ⓘ
let price = dec!(banana);