pub struct IcuRatio(/* private fields */);
Expand description
A ratio type that uses BigInt
as the underlying type.
Implementations§
Trait Implementations§
source§impl AddAssign for IcuRatio
impl AddAssign for IcuRatio
source§fn add_assign(&mut self, rhs: IcuRatio)
fn add_assign(&mut self, rhs: IcuRatio)
Performs the
+=
operation. Read moresource§impl DivAssign for IcuRatio
impl DivAssign for IcuRatio
source§fn div_assign(&mut self, rhs: IcuRatio)
fn div_assign(&mut self, rhs: IcuRatio)
Performs the
/=
operation. Read moresource§impl FromStr for IcuRatio
impl FromStr for IcuRatio
source§fn from_str(number_str: &str) -> Result<IcuRatio, <IcuRatio as FromStr>::Err>
fn from_str(number_str: &str) -> Result<IcuRatio, <IcuRatio as FromStr>::Err>
Converts a string representation of a ratio into an IcuRatio
.
Supported string formats include:
use core::str::FromStr;
use icu::experimental::units::ratio::IcuRatio;
use num_bigint::BigInt;
use num_rational::Ratio;
use num_traits::identities::Zero;
// Fractional notation
let ratio = IcuRatio::from_str("1/2").unwrap();
assert_eq!(
ratio,
IcuRatio::from(Ratio::new(BigInt::from(1), BigInt::from(2)))
);
// Decimal notation
let ratio = IcuRatio::from_str("1.5").unwrap();
assert_eq!(
ratio,
IcuRatio::from(Ratio::new(BigInt::from(3), BigInt::from(2)))
);
// Scientific notation
let ratio = IcuRatio::from_str("1.5E6").unwrap();
assert_eq!(
ratio,
IcuRatio::from(Ratio::from_integer(BigInt::from(1500000)))
);
// Scientific notation with negative exponent
let ratio = IcuRatio::from_str("1.5E-6").unwrap();
assert_eq!(
ratio,
IcuRatio::from(Ratio::new(BigInt::from(15), BigInt::from(10000000)))
);
// Scientific notation with commas
let ratio = IcuRatio::from_str("1,500E6").unwrap();
assert_eq!(
ratio,
IcuRatio::from(Ratio::from_integer(BigInt::from(1500000000)))
);
// Integer notation
let ratio = IcuRatio::from_str("1").unwrap();
assert_eq!(ratio, IcuRatio::from(Ratio::from_integer(BigInt::from(1))));
// Fractional notation with exponent
let ratio = IcuRatio::from_str("1/2E5").unwrap();
assert_eq!(
ratio,
IcuRatio::from(Ratio::from_integer(BigInt::from(50000)))
);
// Negative numbers in fractional notation
let ratio = IcuRatio::from_str("-1/2").unwrap();
assert_eq!(
ratio,
IcuRatio::from(Ratio::new(BigInt::from(-1), BigInt::from(2)))
);
// Negative numbers in decimal notation
let ratio = IcuRatio::from_str("-1.5").unwrap();
assert_eq!(
ratio,
IcuRatio::from(Ratio::new(BigInt::from(-3), BigInt::from(2)))
);
// Negative numbers in scientific notation
let ratio = IcuRatio::from_str("-1.5E6").unwrap();
assert_eq!(
ratio,
IcuRatio::from(Ratio::from_integer(BigInt::from(-1500000)))
);
// Negative numbers in scientific notation with commas
let ratio = IcuRatio::from_str("-1,500E6").unwrap();
assert_eq!(
ratio,
IcuRatio::from(Ratio::from_integer(BigInt::from(-1500000000)))
);
// Corner cases
// Empty string
let ratio = IcuRatio::from_str("").unwrap();
assert_eq!(ratio, IcuRatio::zero());
// Single dot
let ratio = IcuRatio::from_str(".").unwrap();
assert_eq!(ratio, IcuRatio::zero());
// Single minus
let ratio = IcuRatio::from_str("-").unwrap();
assert_eq!(ratio, IcuRatio::zero());
// Single plus
let ratio = IcuRatio::from_str("+").unwrap();
assert_eq!(ratio, IcuRatio::zero());
// Only zeros after dot
let ratio = IcuRatio::from_str(".000").unwrap();
assert_eq!(ratio, IcuRatio::zero());
// Error cases
// Division by zero
assert!(IcuRatio::from_str("1/0").is_err());
// Multiple slashes
assert!(IcuRatio::from_str("1/2/3").is_err());
// Non-numeric characters in fractions
assert!(IcuRatio::from_str("1/2A").is_err());
// Multiple scientific notations
assert!(IcuRatio::from_str("1.5E6E6").is_err());
// Multiple decimal points
assert!(IcuRatio::from_str("1.5.6").is_err());
// Exponent part is not an integer
assert!(IcuRatio::from_str("1.5E6.5").is_err());
NOTE: You can add as many commas as you want in the string, they will be disregarded.
source§type Err = RatioFromStrError
type Err = RatioFromStrError
The associated error which can be returned from parsing.
source§impl MulAssign<&SiPrefix> for IcuRatio
impl MulAssign<&SiPrefix> for IcuRatio
source§fn mul_assign(&mut self, rhs: &SiPrefix)
fn mul_assign(&mut self, rhs: &SiPrefix)
Performs the
*=
operation. Read moresource§impl MulAssign for IcuRatio
impl MulAssign for IcuRatio
source§fn mul_assign(&mut self, rhs: IcuRatio)
fn mul_assign(&mut self, rhs: IcuRatio)
Performs the
*=
operation. Read moresource§impl Ord for IcuRatio
impl Ord for IcuRatio
source§impl PartialOrd for IcuRatio
impl PartialOrd for IcuRatio
source§impl SubAssign for IcuRatio
impl SubAssign for IcuRatio
source§fn sub_assign(&mut self, rhs: IcuRatio)
fn sub_assign(&mut self, rhs: IcuRatio)
Performs the
-=
operation. Read moreimpl Eq for IcuRatio
impl StructuralPartialEq for IcuRatio
Auto Trait Implementations§
impl Freeze for IcuRatio
impl RefUnwindSafe for IcuRatio
impl Send for IcuRatio
impl Sync for IcuRatio
impl Unpin for IcuRatio
impl UnwindSafe for IcuRatio
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more