Skip to main content

Money

Struct Money 

Source
pub struct Money { /* private fields */ }
Expand description

A currency amount stored in Stripe’s smallest denomination.

Stripe never transmits a floating point amount, so Money keeps an integer minor-unit count plus the currency that determines how the value converts back to a major-unit decimal. Two USD is 200 minor units; two JPY is 2, because JPY has no minor unit.

Accessors are generated by #[derive(Getter)]. Both fields are public because callers persist the raw minor-unit count straight into Stripe’s amount parameter; currency_code pairs the two when a human-readable form is needed.

Implementations§

Source§

impl Money

Source

pub const fn zero(currency: Currency) -> Money

Build a zero amount in the given currency.

§Arguments
  • Currency - the currency of the zero amount.
§Returns
  • Self - a zero-valued amount.
Source

pub const fn from_minor(amount: i64, currency: Currency) -> Money

Build an amount directly from a minor-unit value.

This is the constructor that matches the wire format: Stripe sends and accepts amount in the smallest denomination.

§Arguments
  • i64 - the amount in the currency’s smallest denomination.
  • Currency - the currency the amount is denominated in.
§Returns
  • Self - the amount, unchanged.
Source

pub const fn from_major(amount: i64, currency: Currency) -> Money

Build an amount from a major-unit value.

1_099 USD becomes 109_900 minor units; 1_099 JPY stays 1_099 because JPY has no minor unit.

§Arguments
  • i64 - the amount in major units.
  • Currency - the currency the amount is denominated in.
§Returns
  • Self - the amount converted into minor units.
Source

pub fn currency_code(&self) -> &'static str

Return the ISO 4217 code Stripe expects for this amount.

§Returns
  • &'static str - the three-letter uppercase currency code.
Source

pub fn to_major(&self) -> Decimal

Return the amount converted back to major units.

§Returns
  • Decimal - the minor-unit count rescaled by the currency’s exponent, so 200 USD reads back as 2.00.
Source

pub fn with_currency(&self, currency: Currency) -> Money

Return the same minor-unit count labelled with another currency.

Stripe never converts between currencies; this only rewrites the label and exists for presentation code, not for sending a charge.

§Arguments
  • Currency - the currency to relabel the amount with.
§Returns
  • Self - the same minor-unit count under a new currency.
Source

pub fn is_negative(&self) -> bool

Return whether the amount is negative, which Stripe rejects.

§Returns
  • bool - true when the amount is below zero.
Source

pub fn abs(&self) -> Money

Return the amount with any leading minus sign removed.

§Returns
  • Self - the absolute value of the amount.
Source

pub fn checked_add(&self, other: Money) -> Result<Money, StripeParseError>

Return this amount plus another of the same currency.

§Arguments
  • Self - the amount to add.
§Returns
  • Result<Self, StripeParseError> - the sum, or an error when the currencies differ or the addition overflows i64.
Source§

impl Money

Source

pub fn get_amount(&self) -> i64

Source

pub fn get_currency(&self) -> Currency

Trait Implementations§

Source§

impl Clone for Money

Source§

fn clone(&self) -> Money

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Money

Source§

impl Debug for Money

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for Money

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), Error>

Render the amount as a decimal in major units.

§Arguments
  • &mut Formatter<'_> - the formatter to write the value into.
§Returns

A Formatter writing the decimal amount and its currency code.

Source§

impl Eq for Money

Source§

impl Hash for Money

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Money

Source§

fn cmp(&self, other: &Money) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Money

Source§

fn eq(&self, other: &Money) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Money

Source§

fn partial_cmp(&self, other: &Money) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Money

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

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 UnsafeUnpin for Money

§

impl UnwindSafe for Money

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.