pub struct Money {
pub currency_code: String,
pub units: i64,
pub nanos: i32,
}
money
only.Expand description
Represents an amount of money with its currency type.
Fields§
§currency_code: String
The three-letter currency code defined in ISO 4217.
units: i64
The whole units of the amount.
For example if currencyCode
is "USD"
, then 1 unit is one US dollar.
nanos: i32
Number of nano (10^-9) units of the amount.
The value must be between -999,999,999 and +999,999,999 inclusive.
If units
is positive, nanos
must be positive or zero.
If units
is zero, nanos
can be positive, zero, or negative.
If units
is negative, nanos
must be negative or zero.
For example $-1.75 is represented as units
=-1 and nanos
=-750,000,000.
Implementations§
Source§impl Money
impl Money
Sourcepub fn to_formatted_string(&self, symbol: &str, decimal_places: u32) -> String
pub fn to_formatted_string(&self, symbol: &str, decimal_places: u32) -> String
Normalizes the Money
amount and returns a string containing the currency symbol and the monetary amount rounded by the specified decimal places.
Sourcepub fn normalize(self) -> Result<Self, MoneyError>
pub fn normalize(self) -> Result<Self, MoneyError>
Normalizes units and nanos. Fails in case of overflow.
Sourcepub fn new(
currency_code: String,
units: i64,
nanos: i32,
) -> Result<Self, MoneyError>
pub fn new( currency_code: String, units: i64, nanos: i32, ) -> Result<Self, MoneyError>
Creates a new instance, if the normalization does not return errors like Overflow or Underflow.
Sourcepub fn to_rounded_imprecise_f64(
&self,
decimal_places: u32,
) -> Result<f64, MoneyError>
pub fn to_rounded_imprecise_f64( &self, decimal_places: u32, ) -> Result<f64, MoneyError>
Converts the Money
amount into a decimal (f64) representation,
rounded to the specified number of decimal places.
decimal_places
determines the precision of the rounding. For example:
0
rounds to the nearest whole unit.2
rounds to two decimal places (e.g., for cents).
WARNING: The usage of f64
introduces floating-point precision issues. Do not use it for critical financial calculations.
Sourcepub fn as_imprecise_f64(&self) -> f64
pub fn as_imprecise_f64(&self) -> f64
Converts the Money
amount into a decimal (f64) representation.
WARNING: The usage of f64
introduces floating-point precision issues. Do not use it for critical financial calculations.
Sourcepub fn from_imprecise_f64(
currency_code: String,
amount: f64,
) -> Result<Self, MoneyError>
pub fn from_imprecise_f64( currency_code: String, amount: f64, ) -> Result<Self, MoneyError>
Creates a new Money
instance with the given currency code and decimal amount.
This is a convenience constructor that handles splitting a decimal value into units and nanos.
WARNING: The usage of f64
introduces floating-point precision issues. Do not use it for critical financial calculations.
Sourcepub fn try_add(&self, other: &Self) -> Result<Self, MoneyError>
pub fn try_add(&self, other: &Self) -> Result<Self, MoneyError>
Sourcepub fn try_add_assign(&mut self, other: &Self) -> Result<(), MoneyError>
pub fn try_add_assign(&mut self, other: &Self) -> Result<(), MoneyError>
Attempts to add another Money
amount to this one in place.
Returns an error if currencies mismatch or if addition causes an overflow/underflow.
Sourcepub fn try_sub(&self, other: &Self) -> Result<Self, MoneyError>
pub fn try_sub(&self, other: &Self) -> Result<Self, MoneyError>
Sourcepub fn try_sub_assign(&mut self, other: &Self) -> Result<(), MoneyError>
pub fn try_sub_assign(&mut self, other: &Self) -> Result<(), MoneyError>
Attempts to subtract another Money
amount from this one in place.
Returns an error if currencies mismatch or if subtraction causes an overflow/underflow.
Sourcepub fn try_mul_i64(&self, rhs: i64) -> Result<Self, MoneyError>
pub fn try_mul_i64(&self, rhs: i64) -> Result<Self, MoneyError>
Sourcepub fn try_mul_f64(&self, rhs: f64) -> Result<Self, MoneyError>
pub fn try_mul_f64(&self, rhs: f64) -> Result<Self, MoneyError>
Sourcepub fn try_div_i64(&self, rhs: i64) -> Result<Self, MoneyError>
pub fn try_div_i64(&self, rhs: i64) -> Result<Self, MoneyError>
Sourcepub fn try_div_f64(&self, rhs: f64) -> Result<Self, MoneyError>
pub fn try_div_f64(&self, rhs: f64) -> Result<Self, MoneyError>
Attempts to divide this Money
amount by a float scalar, returning a new Money
instance.
Returns an error if the divisor is zero, non-finite, or if division causes an internal conversion error.
WARNING: The usage of f64
introduces floating-point precision issues. Do not use it for critical financial calculations.
Sourcepub fn try_neg(&self) -> Result<Self, MoneyError>
pub fn try_neg(&self) -> Result<Self, MoneyError>
Sourcepub fn is_currency(&self, code: &str) -> bool
pub fn is_currency(&self, code: &str) -> bool
Checks if the money’s currency code matches the given code
.
The code
should be a three-letter ISO 4217 currency code (e.g., “USD”, “EUR”).
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Checks if the money amount is strictly positive (greater than zero).
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Checks if the money amount is strictly negative (less than zero).
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Money
impl<'de> Deserialize<'de> for Money
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>,
Source§impl Message for Money
impl Message for Money
Source§fn encoded_len(&self) -> usize
fn encoded_len(&self) -> usize
Source§fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>where
Self: Sized,
fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>where
Self: Sized,
Source§fn encode_to_vec(&self) -> Vec<u8> ⓘwhere
Self: Sized,
fn encode_to_vec(&self) -> Vec<u8> ⓘwhere
Self: Sized,
Source§fn encode_length_delimited(
&self,
buf: &mut impl BufMut,
) -> Result<(), EncodeError>where
Self: Sized,
fn encode_length_delimited(
&self,
buf: &mut impl BufMut,
) -> Result<(), EncodeError>where
Self: Sized,
Source§fn encode_length_delimited_to_vec(&self) -> Vec<u8> ⓘwhere
Self: Sized,
fn encode_length_delimited_to_vec(&self) -> Vec<u8> ⓘwhere
Self: Sized,
Source§fn decode(buf: impl Buf) -> Result<Self, DecodeError>where
Self: Default,
fn decode(buf: impl Buf) -> Result<Self, DecodeError>where
Self: Default,
Source§fn decode_length_delimited(buf: impl Buf) -> Result<Self, DecodeError>where
Self: Default,
fn decode_length_delimited(buf: impl Buf) -> Result<Self, DecodeError>where
Self: Default,
Source§fn merge(&mut self, buf: impl Buf) -> Result<(), DecodeError>where
Self: Sized,
fn merge(&mut self, buf: impl Buf) -> Result<(), DecodeError>where
Self: Sized,
self
. Read moreSource§fn merge_length_delimited(&mut self, buf: impl Buf) -> Result<(), DecodeError>where
Self: Sized,
fn merge_length_delimited(&mut self, buf: impl Buf) -> Result<(), DecodeError>where
Self: Sized,
self
.Source§impl PartialOrd for Money
impl PartialOrd for Money
impl Eq for Money
impl StructuralPartialEq for Money
Auto Trait Implementations§
impl Freeze for Money
impl RefUnwindSafe for Money
impl Send for Money
impl Sync for Money
impl Unpin for Money
impl UnwindSafe for Money
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T> AnyExt for T
impl<T> AnyExt for T
Source§fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
T
behind referenceSource§fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
T
behind mutable referenceSource§fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
T
behind Rc
pointerSource§fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
T
behind Arc
pointerSource§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
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
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters
when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, X> CoerceTo<T> for Xwhere
T: CoerceFrom<X> + ?Sized,
impl<T, X> CoerceTo<T> for Xwhere
T: CoerceFrom<X> + ?Sized,
fn coerce_rc_to(self: Rc<X>) -> Rc<T>
fn coerce_box_to(self: Box<X>) -> Box<T>
fn coerce_ref_to(&self) -> &T
fn coerce_mut_to(&mut self) -> &mut T
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle
.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other
into Self
, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T
.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters
when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self
into T
, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors
fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds
error is returned which contains
the unclamped color. Read more