Function to_u32_floor

Source
fn to_u32_floor<T>(x: &T) -> u32
where T: FixedPointNumber + Copy + FixedPointInfo, T::Inner: Copy + PartialOrd + TryInto<i128>,
Expand description

Extracts the integer part of a fixed-point number as a u32, truncating the fractional component toward zero.

Fixed-point numbers store their value as inner / DIV, where DIV is a power of 10 (10^9 for 64-bit types, 10^18 for 128-bit types). Dividing inner by DIV removes the fractional portion, leaving the integer part.

§Behavior

ConditionReturns
Integer part negative0
Integer part > u32::MAXu32::MAX
OtherwiseInteger part

§Arguments

  • x - The fixed-point value to truncate.

§Returns

The integer portion of x, clamped to [0, u32::MAX].

§Examples

// FixedU64 with DIV = 10^9: inner value 300_750_000_000 represents 300.75
let x = FixedU64::from_inner(300_750_000_000);
assert_eq!(to_u32_floor(&x), 300);

// Negative values clamp to 0
let x = FixedI64::saturating_from_integer(-5);
assert_eq!(to_u32_floor(&x), 0);