pub struct Percentage(/* private fields */);Expand description
A percentage value validated to the range [0, 100].
Implementations§
Source§impl Percentage
impl Percentage
Sourcepub fn new(value: Decimal) -> Result<Self, PercentageError>
pub fn new(value: Decimal) -> Result<Self, PercentageError>
Create a new percentage, validating that the value is in [0, 100].
Sourcepub fn from_trusted(value: u32) -> Self
pub fn from_trusted(value: u32) -> Self
Sourcepub fn as_fraction(&self) -> Decimal
pub fn as_fraction(&self) -> Decimal
Convert to a fraction in [0.0, 1.0] (raw Decimal).
Sourcepub fn to_fraction(&self) -> Fraction
pub fn to_fraction(&self) -> Fraction
Convert to a validated Fraction value.
Sourcepub fn is_likely_fraction(&self) -> bool
pub fn is_likely_fraction(&self) -> bool
Returns true if this value looks like it was passed as a fraction (0-1)
instead of a percentage (0-100).
Values < 1.0 are suspiciously small for most percentage use cases (stop loss, margin requirement, risk limit).
§Example
use rust_decimal_macros::dec;
use quant_primitives::Percentage;
let pct = Percentage::new(dec!(0.05)).unwrap();
assert!(pct.is_likely_fraction()); // 0.05% is suspicious — caller probably meant 5%
let normal = Percentage::new(dec!(5)).unwrap();
assert!(!normal.is_likely_fraction()); // 5% is plausibleTrait Implementations§
Source§impl Clone for Percentage
impl Clone for Percentage
Source§fn clone(&self) -> Percentage
fn clone(&self) -> Percentage
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for Percentage
impl Debug for Percentage
Source§impl<'de> Deserialize<'de> for Percentage
impl<'de> Deserialize<'de> for Percentage
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Display for Percentage
impl Display for Percentage
Source§impl Ord for Percentage
impl Ord for Percentage
Source§fn cmp(&self, other: &Percentage) -> Ordering
fn cmp(&self, other: &Percentage) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq for Percentage
impl PartialEq for Percentage
Source§impl PartialOrd for Percentage
impl PartialOrd for Percentage
Source§impl Serialize for Percentage
impl Serialize for Percentage
impl Copy for Percentage
impl Eq for Percentage
impl StructuralPartialEq for Percentage
Auto Trait Implementations§
impl Freeze for Percentage
impl RefUnwindSafe for Percentage
impl Send for Percentage
impl Sync for Percentage
impl Unpin for Percentage
impl UnsafeUnpin for Percentage
impl UnwindSafe for Percentage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more