pub struct SignedAmount(/* private fields */);Expand description
A signed amount.
The SignedAmount type can be used to express Bitcoin amounts that support arithmetic and
conversion to various denominations. The SignedAmount type does not implement serde
traits but we do provide modules for serializing as satoshis or bitcoin.
Warning!
This type implements several arithmetic operations from core::ops.
To prevent errors due to an overflow when using these operations,
it is advised to instead use the checked arithmetic methods whose names
start with checked_. The operations from core::ops that SignedAmount
implements will panic when an overflow occurs.
§Examples
use serde::{Serialize, Deserialize};
use bitcoin_units::SignedAmount;
#[derive(Serialize, Deserialize)]
struct Foo {
// If you are using `rust-bitcoin` then `bitcoin::amount::serde::as_sat` also works.
#[serde(with = "bitcoin_units::amount::serde::as_sat")] // Also `serde::as_btc`.
amount: SignedAmount,
}Implementations§
Source§impl SignedAmount
impl SignedAmount
Sourcepub const fn to_sat(self) -> i64
pub const fn to_sat(self) -> i64
Gets the number of satoshis in this SignedAmount.
§Examples
assert_eq!(SignedAmount::ONE_BTC.to_sat(), 100_000_000);Sourcepub const fn from_sat(satoshi: i64) -> Result<Self, OutOfRangeError>
pub const fn from_sat(satoshi: i64) -> Result<Self, OutOfRangeError>
Constructs a new SignedAmount from the given number of satoshis.
§Errors
If satoshi is outside of valid range (see Self::MAX_MONEY).
§Examples
let amount = SignedAmount::from_sat(sat)?;
assert_eq!(amount.to_sat(), sat);Source§impl SignedAmount
impl SignedAmount
Sourcepub const MAX_MONEY: Self = Self::MAX
pub const MAX_MONEY: Self = Self::MAX
The maximum value allowed as an amount. Useful for sanity checking.
Sourcepub const fn from_sat_i32(satoshi: i32) -> Self
pub const fn from_sat_i32(satoshi: i32) -> Self
Constructs a new SignedAmount with satoshi precision and the given number of satoshis.
Accepts an i32 which is guaranteed to be in range for the type, but which can only
represent roughly -21.47 to 21.47 BTC.
Sourcepub fn from_btc(btc: f64) -> Result<Self, ParseAmountError>
pub fn from_btc(btc: f64) -> Result<Self, ParseAmountError>
Converts from a value expressing a decimal number of bitcoin to a SignedAmount.
§Errors
If the amount is too big (positive or negative) or too precise.
Please be aware of the risk of using floating-point numbers.
§Examples
let amount = SignedAmount::from_btc(-0.01)?;
assert_eq!(amount.to_sat(), -1_000_000);Sourcepub fn from_int_btc<T: Into<i16>>(whole_bitcoin: T) -> Self
pub fn from_int_btc<T: Into<i16>>(whole_bitcoin: T) -> Self
Converts from a value expressing a whole number of bitcoin to a SignedAmount.
Sourcepub const fn from_btc_i16(whole_bitcoin: i16) -> Self
pub const fn from_btc_i16(whole_bitcoin: i16) -> Self
Converts from a value expressing a whole number of bitcoin to a SignedAmount
in const context.
Sourcepub fn from_str_in(
s: &str,
denom: Denomination,
) -> Result<Self, ParseAmountError>
pub fn from_str_in( s: &str, denom: Denomination, ) -> Result<Self, ParseAmountError>
Parses a decimal string as a value in the given Denomination.
Note: This only parses the value string. If you want to parse a string
containing the value with denomination, use FromStr.
§Errors
If the amount is too big (positive or negative) or too precise.
Sourcepub fn from_str_with_denomination(s: &str) -> Result<Self, ParseError>
pub fn from_str_with_denomination(s: &str) -> Result<Self, ParseError>
Parses amounts with denomination suffix as produced by Self::to_string_with_denomination
or with fmt::Display.
If you want to parse only the amount without the denomination, use Self::from_str_in.
§Errors
If the amount is too big (positive or negative) or too precise.
§Examples
let amount = SignedAmount::from_str_with_denomination("0.1 BTC")?;
assert_eq!(amount, SignedAmount::from_sat(10_000_000)?);Sourcepub fn to_float_in(self, denom: Denomination) -> f64
pub fn to_float_in(self, denom: Denomination) -> f64
Expresses this SignedAmount as a floating-point value in the given Denomination.
Please be aware of the risk of using floating-point numbers.
§Examples
let amount = SignedAmount::from_sat(100_000)?;
assert_eq!(amount.to_float_in(Denomination::Bitcoin), 0.001);Sourcepub fn to_btc(self) -> f64
pub fn to_btc(self) -> f64
Expresses this SignedAmount as a floating-point value in Bitcoin.
Please be aware of the risk of using floating-point numbers.
§Examples
let amount = SignedAmount::from_sat(100_000)?;
assert_eq!(amount.to_btc(), amount.to_float_in(Denomination::Bitcoin));Sourcepub fn from_float_in(
value: f64,
denom: Denomination,
) -> Result<Self, ParseAmountError>
pub fn from_float_in( value: f64, denom: Denomination, ) -> Result<Self, ParseAmountError>
Converts this SignedAmount in floating-point notation in the given Denomination.
§Errors
If the amount is too big (positive or negative) or too precise.
Please be aware of the risk of using floating-point numbers.
Sourcepub fn display_in(self, denomination: Denomination) -> Display
pub fn display_in(self, denomination: Denomination) -> Display
Constructs a new object that implements fmt::Display in the given Denomination.
This function is useful if you do not wish to allocate. See also Self::to_string_in.
§Examples
let amount = SignedAmount::from_sat(10_000_000)?;
let mut output = String::new();
let _ = write!(&mut output, "{}", amount.display_in(Denomination::Bitcoin));
assert_eq!(output, "0.1");Sourcepub fn display_dynamic(self) -> Display
pub fn display_dynamic(self) -> Display
Constructs a new object that implements fmt::Display dynamically selecting
Denomination.
This will use BTC for values greater than or equal to 1 BTC and satoshis otherwise. To avoid confusion the denomination is always shown.
Sourcepub fn to_string_in(self, denom: Denomination) -> String
pub fn to_string_in(self, denom: Denomination) -> String
Returns a formatted string representing this SignedAmount in the given Denomination.
Returned string does not include the denomination.
§Examples
let amount = SignedAmount::from_sat(10_000_000)?;
assert_eq!(amount.to_string_in(Denomination::Bitcoin), "0.1");Sourcepub fn to_string_with_denomination(self, denom: Denomination) -> String
pub fn to_string_with_denomination(self, denom: Denomination) -> String
Returns a formatted string representing this SignedAmount in the given Denomination,
suffixed with the abbreviation for the denomination.
§Examples
let amount = SignedAmount::from_sat(10_000_000)?;
assert_eq!(amount.to_string_with_denomination(Denomination::Bitcoin), "0.1 BTC");Sourcepub const fn abs(self) -> Self
pub const fn abs(self) -> Self
Gets the absolute value of this SignedAmount.
This function never overflows or panics, unlike i64::abs().
Sourcepub fn unsigned_abs(self) -> Amount
pub fn unsigned_abs(self) -> Amount
Gets the absolute value of this SignedAmount returning Amount.
Sourcepub fn signum(self) -> i64
pub fn signum(self) -> i64
Returns a number representing sign of this SignedAmount.
0if the amount is zero1if the amount is positive-1if the amount is negative
Sourcepub fn is_positive(self) -> bool
pub fn is_positive(self) -> bool
Checks if this SignedAmount is positive.
Returns true if this SignedAmount is positive and false if
this SignedAmount is zero or negative.
Sourcepub fn is_negative(self) -> bool
pub fn is_negative(self) -> bool
Checks if this SignedAmount is negative.
Returns true if this SignedAmount is negative and false if
this SignedAmount is zero or positive.
Sourcepub const fn checked_abs(self) -> Option<Self>
👎Deprecated since 1.0.0-rc.0: Never returns none, use abs() instead
pub const fn checked_abs(self) -> Option<Self>
abs() insteadReturns the absolute value of this SignedAmount.
Consider using unsigned_abs which is often more practical.
Returns None if overflow occurred. (self == i64::MIN)
Sourcepub const fn checked_add(self, rhs: Self) -> Option<Self>
pub const fn checked_add(self, rhs: Self) -> Option<Self>
Checked addition.
Returns None if the sum is above SignedAmount::MAX or below SignedAmount::MIN.
Sourcepub const fn checked_sub(self, rhs: Self) -> Option<Self>
pub const fn checked_sub(self, rhs: Self) -> Option<Self>
Checked subtraction.
Returns None if the difference is above SignedAmount::MAX or below
SignedAmount::MIN.
Sourcepub const fn checked_mul(self, rhs: i64) -> Option<Self>
pub const fn checked_mul(self, rhs: i64) -> Option<Self>
Checked multiplication.
Returns None if the product is above SignedAmount::MAX or below
SignedAmount::MIN.
Sourcepub const fn checked_div(self, rhs: i64) -> Option<Self>
pub const fn checked_div(self, rhs: i64) -> Option<Self>
Checked integer division.
Be aware that integer division loses the remainder if no exact division can be made.
Returns None if overflow occurred.
Sourcepub const fn checked_rem(self, rhs: i64) -> Option<Self>
pub const fn checked_rem(self, rhs: i64) -> Option<Self>
Checked remainder.
Returns None if overflow occurred.
Sourcepub fn positive_sub(self, rhs: Self) -> Option<Self>
pub fn positive_sub(self, rhs: Self) -> Option<Self>
Subtraction that doesn’t allow negative SignedAmounts.
Returns None if either self, rhs or the result is strictly negative.
Sourcepub fn to_unsigned(self) -> Result<Amount, OutOfRangeError>
pub fn to_unsigned(self) -> Result<Amount, OutOfRangeError>
Trait Implementations§
Source§impl<'a> Add<&'a NumOpResult<SignedAmount>> for &SignedAmount
impl<'a> Add<&'a NumOpResult<SignedAmount>> for &SignedAmount
Source§type Output = <SignedAmount as Add<NumOpResult<SignedAmount>>>::Output
type Output = <SignedAmount as Add<NumOpResult<SignedAmount>>>::Output
+ operator.Source§fn add(self, rhs: &NumOpResult<SignedAmount>) -> Self::Output
fn add(self, rhs: &NumOpResult<SignedAmount>) -> Self::Output
+ operation. Read moreSource§impl Add<&NumOpResult<SignedAmount>> for SignedAmount
impl Add<&NumOpResult<SignedAmount>> for SignedAmount
Source§type Output = <SignedAmount as Add<NumOpResult<SignedAmount>>>::Output
type Output = <SignedAmount as Add<NumOpResult<SignedAmount>>>::Output
+ operator.Source§fn add(self, rhs: &NumOpResult<SignedAmount>) -> Self::Output
fn add(self, rhs: &NumOpResult<SignedAmount>) -> Self::Output
+ operation. Read moreSource§impl<'a> Add<&'a SignedAmount> for &SignedAmount
impl<'a> Add<&'a SignedAmount> for &SignedAmount
Source§impl Add<&SignedAmount> for SignedAmount
impl Add<&SignedAmount> for SignedAmount
Source§impl Add<NumOpResult<SignedAmount>> for &SignedAmount
impl Add<NumOpResult<SignedAmount>> for &SignedAmount
Source§type Output = <SignedAmount as Add<NumOpResult<SignedAmount>>>::Output
type Output = <SignedAmount as Add<NumOpResult<SignedAmount>>>::Output
+ operator.Source§fn add(self, rhs: NumOpResult<SignedAmount>) -> Self::Output
fn add(self, rhs: NumOpResult<SignedAmount>) -> Self::Output
+ operation. Read moreSource§impl Add<NumOpResult<SignedAmount>> for SignedAmount
impl Add<NumOpResult<SignedAmount>> for SignedAmount
Source§type Output = NumOpResult<SignedAmount>
type Output = NumOpResult<SignedAmount>
+ operator.Source§fn add(self, rhs: NumOpResult<SignedAmount>) -> Self::Output
fn add(self, rhs: NumOpResult<SignedAmount>) -> Self::Output
+ operation. Read moreSource§impl Add<SignedAmount> for &SignedAmount
impl Add<SignedAmount> for &SignedAmount
Source§impl Add for SignedAmount
impl Add for SignedAmount
Source§type Output = NumOpResult<SignedAmount>
type Output = NumOpResult<SignedAmount>
+ operator.Source§impl AddAssign<SignedAmount> for NumOpResult<SignedAmount>
impl AddAssign<SignedAmount> for NumOpResult<SignedAmount>
Source§fn add_assign(&mut self, rhs: SignedAmount)
fn add_assign(&mut self, rhs: SignedAmount)
+= operation. Read moreSource§impl<'a> Arbitrary<'a> for SignedAmount
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for SignedAmount
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl Binary for SignedAmount
impl Binary for SignedAmount
Source§impl Clone for SignedAmount
impl Clone for SignedAmount
Source§fn clone(&self) -> SignedAmount
fn clone(&self) -> SignedAmount
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SignedAmount
impl Debug for SignedAmount
Source§impl Default for SignedAmount
impl Default for SignedAmount
Source§impl Display for SignedAmount
impl Display for SignedAmount
Source§impl<'a> Div<&'a SignedAmount> for &SignedAmount
impl<'a> Div<&'a SignedAmount> for &SignedAmount
Source§impl Div<&SignedAmount> for SignedAmount
impl Div<&SignedAmount> for SignedAmount
Source§impl<'a> Div<&'a i64> for &SignedAmount
impl<'a> Div<&'a i64> for &SignedAmount
Source§impl Div<&i64> for SignedAmount
impl Div<&i64> for SignedAmount
Source§impl Div<NonZero<i64>> for SignedAmount
impl Div<NonZero<i64>> for SignedAmount
Source§type Output = SignedAmount
type Output = SignedAmount
/ operator.Source§impl Div<SignedAmount> for &SignedAmount
impl Div<SignedAmount> for &SignedAmount
Source§impl Div<i64> for &SignedAmount
impl Div<i64> for &SignedAmount
Source§impl Div<i64> for SignedAmount
impl Div<i64> for SignedAmount
Source§impl Div for SignedAmount
impl Div for SignedAmount
Source§impl From<&SignedAmount> for NumOpResult<SignedAmount>
impl From<&SignedAmount> for NumOpResult<SignedAmount>
Source§fn from(a: &SignedAmount) -> Self
fn from(a: &SignedAmount) -> Self
Source§impl From<Amount> for SignedAmount
impl From<Amount> for SignedAmount
Source§impl From<SignedAmount> for NumOpResult<SignedAmount>
impl From<SignedAmount> for NumOpResult<SignedAmount>
Source§fn from(a: SignedAmount) -> Self
fn from(a: SignedAmount) -> Self
Source§impl FromStr for SignedAmount
impl FromStr for SignedAmount
Source§impl Hash for SignedAmount
impl Hash for SignedAmount
Source§impl LowerHex for SignedAmount
impl LowerHex for SignedAmount
Source§impl<'a> Mul<&'a SignedAmount> for &i64
impl<'a> Mul<&'a SignedAmount> for &i64
Source§impl Mul<&SignedAmount> for i64
impl Mul<&SignedAmount> for i64
Source§impl<'a> Mul<&'a i64> for &SignedAmount
impl<'a> Mul<&'a i64> for &SignedAmount
Source§impl Mul<&i64> for SignedAmount
impl Mul<&i64> for SignedAmount
Source§impl Mul<SignedAmount> for &i64
impl Mul<SignedAmount> for &i64
Source§impl Mul<SignedAmount> for i64
impl Mul<SignedAmount> for i64
Source§type Output = NumOpResult<SignedAmount>
type Output = NumOpResult<SignedAmount>
* operator.Source§impl Mul<i64> for &SignedAmount
impl Mul<i64> for &SignedAmount
Source§impl Mul<i64> for SignedAmount
impl Mul<i64> for SignedAmount
Source§impl Neg for SignedAmount
impl Neg for SignedAmount
Source§impl Octal for SignedAmount
impl Octal for SignedAmount
Source§impl Ord for SignedAmount
impl Ord for SignedAmount
Source§fn cmp(&self, other: &SignedAmount) -> Ordering
fn cmp(&self, other: &SignedAmount) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for SignedAmount
impl PartialEq for SignedAmount
Source§impl PartialOrd for SignedAmount
impl PartialOrd for SignedAmount
Source§impl<'a> Rem<&'a i64> for &SignedAmount
impl<'a> Rem<&'a i64> for &SignedAmount
Source§impl Rem<&i64> for SignedAmount
impl Rem<&i64> for SignedAmount
Source§impl Rem<i64> for &SignedAmount
impl Rem<i64> for &SignedAmount
Source§impl Rem<i64> for SignedAmount
impl Rem<i64> for SignedAmount
Source§impl<'a> Sub<&'a NumOpResult<SignedAmount>> for &SignedAmount
impl<'a> Sub<&'a NumOpResult<SignedAmount>> for &SignedAmount
Source§type Output = <SignedAmount as Sub<NumOpResult<SignedAmount>>>::Output
type Output = <SignedAmount as Sub<NumOpResult<SignedAmount>>>::Output
- operator.Source§fn sub(self, rhs: &NumOpResult<SignedAmount>) -> Self::Output
fn sub(self, rhs: &NumOpResult<SignedAmount>) -> Self::Output
- operation. Read moreSource§impl Sub<&NumOpResult<SignedAmount>> for SignedAmount
impl Sub<&NumOpResult<SignedAmount>> for SignedAmount
Source§type Output = <SignedAmount as Sub<NumOpResult<SignedAmount>>>::Output
type Output = <SignedAmount as Sub<NumOpResult<SignedAmount>>>::Output
- operator.Source§fn sub(self, rhs: &NumOpResult<SignedAmount>) -> Self::Output
fn sub(self, rhs: &NumOpResult<SignedAmount>) -> Self::Output
- operation. Read moreSource§impl<'a> Sub<&'a SignedAmount> for &SignedAmount
impl<'a> Sub<&'a SignedAmount> for &SignedAmount
Source§impl Sub<&SignedAmount> for SignedAmount
impl Sub<&SignedAmount> for SignedAmount
Source§impl Sub<NumOpResult<SignedAmount>> for &SignedAmount
impl Sub<NumOpResult<SignedAmount>> for &SignedAmount
Source§type Output = <SignedAmount as Sub<NumOpResult<SignedAmount>>>::Output
type Output = <SignedAmount as Sub<NumOpResult<SignedAmount>>>::Output
- operator.Source§fn sub(self, rhs: NumOpResult<SignedAmount>) -> Self::Output
fn sub(self, rhs: NumOpResult<SignedAmount>) -> Self::Output
- operation. Read moreSource§impl Sub<NumOpResult<SignedAmount>> for SignedAmount
impl Sub<NumOpResult<SignedAmount>> for SignedAmount
Source§type Output = NumOpResult<SignedAmount>
type Output = NumOpResult<SignedAmount>
- operator.Source§fn sub(self, rhs: NumOpResult<SignedAmount>) -> Self::Output
fn sub(self, rhs: NumOpResult<SignedAmount>) -> Self::Output
- operation. Read moreSource§impl Sub<SignedAmount> for &SignedAmount
impl Sub<SignedAmount> for &SignedAmount
Source§impl Sub for SignedAmount
impl Sub for SignedAmount
Source§type Output = NumOpResult<SignedAmount>
type Output = NumOpResult<SignedAmount>
- operator.Source§impl SubAssign<SignedAmount> for NumOpResult<SignedAmount>
impl SubAssign<SignedAmount> for NumOpResult<SignedAmount>
Source§fn sub_assign(&mut self, rhs: SignedAmount)
fn sub_assign(&mut self, rhs: SignedAmount)
-= operation. Read more