pub struct SignedDecimal(/* private fields */);
Expand description
A signed fixed-point decimal value with 18 fractional digits, i.e. SignedDecimal(1_000_000_000_000_000_000) == 1.0
The greatest possible value that can be represented is 170141183460469231731.687303715884105727 (which is (2^127 - 1) / 10^18) and the smallest is -170141183460469231731.687303715884105728 (which is -2^127 / 10^18).
Implementations§
Source§impl SignedDecimal
impl SignedDecimal
Sourcepub const DECIMAL_PLACES: u32 = 18u32
pub const DECIMAL_PLACES: u32 = 18u32
The number of decimal places. Since decimal types are fixed-point rather than floating-point, this is a constant.
Sourcepub const MAX: Self
pub const MAX: Self
The largest value that can be represented by this signed decimal type.
§Examples
assert_eq!(SignedDecimal::MAX.to_string(), "170141183460469231731.687303715884105727");
Sourcepub const MIN: Self
pub const MIN: Self
The smallest value that can be represented by this signed decimal type.
§Examples
assert_eq!(SignedDecimal::MIN.to_string(), "-170141183460469231731.687303715884105728");
Sourcepub const fn new(value: Int128) -> Self
pub const fn new(value: Int128) -> Self
Creates a SignedDecimal(value)
This is equivalent to SignedDecimal::from_atomics(value, 18)
but usable in a const context.
§Examples
assert_eq!(SignedDecimal::new(Int128::one()).to_string(), "0.000000000000000001");
Sourcepub const fn raw(value: i128) -> Self
pub const fn raw(value: i128) -> Self
Creates a SignedDecimal(Int128(value))
This is equivalent to SignedDecimal::from_atomics(value, 18)
but usable in a const context.
§Examples
assert_eq!(SignedDecimal::raw(1234i128).to_string(), "0.000000000000001234");
Sourcepub const fn negative_one() -> Self
pub const fn negative_one() -> Self
Create a -1.0 SignedDecimal
Sourcepub fn from_atomics(
atomics: impl Into<Int128>,
decimal_places: u32,
) -> Result<Self, SignedDecimalRangeExceeded>
pub fn from_atomics( atomics: impl Into<Int128>, decimal_places: u32, ) -> Result<Self, SignedDecimalRangeExceeded>
Creates a signed decimal from a number of atomic units and the number of decimal places. The inputs will be converted internally to form a signed decimal with 18 decimal places. So the input 123 and 2 will create the decimal 1.23.
Using 18 decimal places is slightly more efficient than other values as no internal conversion is necessary.
§Examples
let a = SignedDecimal::from_atomics(Int128::new(1234), 3).unwrap();
assert_eq!(a.to_string(), "1.234");
let a = SignedDecimal::from_atomics(1234i128, 0).unwrap();
assert_eq!(a.to_string(), "1234");
let a = SignedDecimal::from_atomics(1i64, 18).unwrap();
assert_eq!(a.to_string(), "0.000000000000000001");
let a = SignedDecimal::from_atomics(-1i64, 18).unwrap();
assert_eq!(a.to_string(), "-0.000000000000000001");
Sourcepub fn from_ratio(
numerator: impl Into<Int128>,
denominator: impl Into<Int128>,
) -> Self
pub fn from_ratio( numerator: impl Into<Int128>, denominator: impl Into<Int128>, ) -> Self
Returns the ratio (numerator / denominator) as a SignedDecimal
§Examples
assert_eq!(
SignedDecimal::from_ratio(1, 3).to_string(),
"0.333333333333333333"
);
Sourcepub fn checked_from_ratio(
numerator: impl Into<Int128>,
denominator: impl Into<Int128>,
) -> Result<Self, CheckedFromRatioError>
pub fn checked_from_ratio( numerator: impl Into<Int128>, denominator: impl Into<Int128>, ) -> Result<Self, CheckedFromRatioError>
Returns the ratio (numerator / denominator) as a SignedDecimal
§Examples
assert_eq!(
SignedDecimal::checked_from_ratio(1, 3).unwrap().to_string(),
"0.333333333333333333"
);
assert_eq!(
SignedDecimal::checked_from_ratio(1, 0),
Err(CheckedFromRatioError::DivideByZero)
);
Sourcepub const fn is_negative(&self) -> bool
pub const fn is_negative(&self) -> bool
Returns true
if the number is negative (< 0)
Sourcepub const fn atomics(&self) -> Int128
pub const fn atomics(&self) -> Int128
A decimal is an integer of atomic units plus a number that specifies the position of the decimal dot. So any decimal can be expressed as two numbers.
§Examples
// Value with whole and fractional part
let a = SignedDecimal::from_str("1.234").unwrap();
assert_eq!(a.decimal_places(), 18);
assert_eq!(a.atomics(), Int128::new(1234000000000000000));
// Smallest possible value
let b = SignedDecimal::from_str("0.000000000000000001").unwrap();
assert_eq!(b.decimal_places(), 18);
assert_eq!(b.atomics(), Int128::new(1));
Sourcepub const fn decimal_places(&self) -> u32
pub const fn decimal_places(&self) -> u32
The number of decimal places. This is a constant value for now but this could potentially change as the type evolves.
See also SignedDecimal::atomics()
.
Sourcepub fn trunc(&self) -> Self
pub fn trunc(&self) -> Self
Rounds value by truncating the decimal places.
§Examples
assert!(SignedDecimal::from_str("0.6").unwrap().trunc().is_zero());
assert_eq!(SignedDecimal::from_str("-5.8").unwrap().trunc().to_string(), "-5");
Sourcepub fn floor(&self) -> Self
pub fn floor(&self) -> Self
Rounds value down after decimal places. Panics on overflow.
§Examples
assert!(SignedDecimal::from_str("0.6").unwrap().floor().is_zero());
assert_eq!(SignedDecimal::from_str("-5.2").unwrap().floor().to_string(), "-6");
Sourcepub fn checked_floor(&self) -> Result<Self, RoundDownOverflowError>
pub fn checked_floor(&self) -> Result<Self, RoundDownOverflowError>
Rounds value down after decimal places.
Sourcepub fn ceil(&self) -> Self
pub fn ceil(&self) -> Self
Rounds value up after decimal places. Panics on overflow.
§Examples
assert_eq!(SignedDecimal::from_str("0.2").unwrap().ceil(), SignedDecimal::one());
assert_eq!(SignedDecimal::from_str("-5.8").unwrap().ceil().to_string(), "-5");
Sourcepub fn checked_ceil(&self) -> Result<Self, RoundUpOverflowError>
pub fn checked_ceil(&self) -> Result<Self, RoundUpOverflowError>
Rounds value up after decimal places. Returns OverflowError on overflow.
Sourcepub fn checked_add(self, other: Self) -> Result<Self, OverflowError>
pub fn checked_add(self, other: Self) -> Result<Self, OverflowError>
Computes self + other
, returning an OverflowError
if an overflow occurred.
Sourcepub fn checked_sub(self, other: Self) -> Result<Self, OverflowError>
pub fn checked_sub(self, other: Self) -> Result<Self, OverflowError>
Computes self - other
, returning an OverflowError
if an overflow occurred.
Sourcepub fn checked_mul(self, other: Self) -> Result<Self, OverflowError>
pub fn checked_mul(self, other: Self) -> Result<Self, OverflowError>
Multiplies one SignedDecimal
by another, returning an OverflowError
if an overflow occurred.
Sourcepub fn pow(self, exp: u32) -> Self
pub fn pow(self, exp: u32) -> Self
Raises a value to the power of exp
, panics if an overflow occurred.
Sourcepub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError>
pub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError>
Raises a value to the power of exp
, returning an OverflowError
if an overflow occurred.
pub fn checked_div(self, other: Self) -> Result<Self, CheckedFromRatioError>
Sourcepub fn checked_rem(self, other: Self) -> Result<Self, DivideByZeroError>
pub fn checked_rem(self, other: Self) -> Result<Self, DivideByZeroError>
Computes self % other
, returning an DivideByZeroError
if other == 0
.
pub const fn abs_diff(self, other: Self) -> Decimal
pub fn saturating_add(self, other: Self) -> Self
pub fn saturating_sub(self, other: Self) -> Self
pub fn saturating_mul(self, other: Self) -> Self
pub fn saturating_pow(self, exp: u32) -> Self
Sourcepub fn to_int_floor(self) -> Int128
pub fn to_int_floor(self) -> Int128
Converts this decimal to a signed integer by rounding down to the next integer, e.g. 22.5 becomes 22 and -1.2 becomes -2.
§Examples
use core::str::FromStr;
use cosmwasm_std::{SignedDecimal, Int128};
let d = SignedDecimal::from_str("12.345").unwrap();
assert_eq!(d.to_int_floor(), Int128::new(12));
let d = SignedDecimal::from_str("-12.999").unwrap();
assert_eq!(d.to_int_floor(), Int128::new(-13));
let d = SignedDecimal::from_str("-0.05").unwrap();
assert_eq!(d.to_int_floor(), Int128::new(-1));
Sourcepub fn to_int_trunc(self) -> Int128
pub fn to_int_trunc(self) -> Int128
Converts this decimal to a signed integer by truncating the fractional part, e.g. 22.5 becomes 22.
§Examples
use core::str::FromStr;
use cosmwasm_std::{SignedDecimal, Int128};
let d = SignedDecimal::from_str("12.345").unwrap();
assert_eq!(d.to_int_trunc(), Int128::new(12));
let d = SignedDecimal::from_str("-12.999").unwrap();
assert_eq!(d.to_int_trunc(), Int128::new(-12));
let d = SignedDecimal::from_str("75.0").unwrap();
assert_eq!(d.to_int_trunc(), Int128::new(75));
Sourcepub fn to_int_ceil(self) -> Int128
pub fn to_int_ceil(self) -> Int128
Converts this decimal to a signed integer by rounding up to the next integer, e.g. 22.3 becomes 23 and -1.2 becomes -1.
§Examples
use core::str::FromStr;
use cosmwasm_std::{SignedDecimal, Int128};
let d = SignedDecimal::from_str("12.345").unwrap();
assert_eq!(d.to_int_ceil(), Int128::new(13));
let d = SignedDecimal::from_str("-12.999").unwrap();
assert_eq!(d.to_int_ceil(), Int128::new(-12));
let d = SignedDecimal::from_str("75.0").unwrap();
assert_eq!(d.to_int_ceil(), Int128::new(75));
Trait Implementations§
Source§impl Add<&SignedDecimal> for &SignedDecimal
impl Add<&SignedDecimal> for &SignedDecimal
Source§type Output = <SignedDecimal as Add>::Output
type Output = <SignedDecimal as Add>::Output
+
operator.Source§fn add(
self,
other: &SignedDecimal,
) -> <SignedDecimal as Add<SignedDecimal>>::Output
fn add( self, other: &SignedDecimal, ) -> <SignedDecimal as Add<SignedDecimal>>::Output
+
operation. Read moreSource§impl Add<&SignedDecimal> for SignedDecimal
impl Add<&SignedDecimal> for SignedDecimal
Source§type Output = <SignedDecimal as Add>::Output
type Output = <SignedDecimal as Add>::Output
+
operator.Source§fn add(
self,
other: &SignedDecimal,
) -> <SignedDecimal as Add<SignedDecimal>>::Output
fn add( self, other: &SignedDecimal, ) -> <SignedDecimal as Add<SignedDecimal>>::Output
+
operation. Read moreSource§impl<'a> Add<SignedDecimal> for &'a SignedDecimal
impl<'a> Add<SignedDecimal> for &'a SignedDecimal
Source§type Output = <SignedDecimal as Add>::Output
type Output = <SignedDecimal as Add>::Output
+
operator.Source§fn add(
self,
other: SignedDecimal,
) -> <SignedDecimal as Add<SignedDecimal>>::Output
fn add( self, other: SignedDecimal, ) -> <SignedDecimal as Add<SignedDecimal>>::Output
+
operation. Read moreSource§impl Add for SignedDecimal
impl Add for SignedDecimal
Source§impl AddAssign<&SignedDecimal> for SignedDecimal
impl AddAssign<&SignedDecimal> for SignedDecimal
Source§fn add_assign(&mut self, other: &SignedDecimal)
fn add_assign(&mut self, other: &SignedDecimal)
+=
operation. Read moreSource§impl AddAssign for SignedDecimal
impl AddAssign for SignedDecimal
Source§fn add_assign(&mut self, rhs: SignedDecimal)
fn add_assign(&mut self, rhs: SignedDecimal)
+=
operation. Read moreSource§impl Clone for SignedDecimal
impl Clone for SignedDecimal
Source§fn clone(&self) -> SignedDecimal
fn clone(&self) -> SignedDecimal
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for SignedDecimal
impl Debug for SignedDecimal
Source§impl Default for SignedDecimal
impl Default for SignedDecimal
Source§fn default() -> SignedDecimal
fn default() -> SignedDecimal
Source§impl<'de> Deserialize<'de> for SignedDecimal
Deserializes as a base64 string
impl<'de> Deserialize<'de> for SignedDecimal
Deserializes as a base64 string
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 Display for SignedDecimal
impl Display for SignedDecimal
Source§impl Div<&SignedDecimal> for &SignedDecimal
impl Div<&SignedDecimal> for &SignedDecimal
Source§type Output = <SignedDecimal as Div>::Output
type Output = <SignedDecimal as Div>::Output
/
operator.Source§fn div(
self,
other: &SignedDecimal,
) -> <SignedDecimal as Div<SignedDecimal>>::Output
fn div( self, other: &SignedDecimal, ) -> <SignedDecimal as Div<SignedDecimal>>::Output
/
operation. Read moreSource§impl Div<&SignedDecimal> for SignedDecimal
impl Div<&SignedDecimal> for SignedDecimal
Source§type Output = <SignedDecimal as Div>::Output
type Output = <SignedDecimal as Div>::Output
/
operator.Source§fn div(
self,
other: &SignedDecimal,
) -> <SignedDecimal as Div<SignedDecimal>>::Output
fn div( self, other: &SignedDecimal, ) -> <SignedDecimal as Div<SignedDecimal>>::Output
/
operation. Read moreSource§impl Div<Int128> for SignedDecimal
impl Div<Int128> for SignedDecimal
Source§impl<'a> Div<SignedDecimal> for &'a SignedDecimal
impl<'a> Div<SignedDecimal> for &'a SignedDecimal
Source§type Output = <SignedDecimal as Div>::Output
type Output = <SignedDecimal as Div>::Output
/
operator.Source§fn div(
self,
other: SignedDecimal,
) -> <SignedDecimal as Div<SignedDecimal>>::Output
fn div( self, other: SignedDecimal, ) -> <SignedDecimal as Div<SignedDecimal>>::Output
/
operation. Read moreSource§impl Div for SignedDecimal
impl Div for SignedDecimal
Source§impl DivAssign<&SignedDecimal> for SignedDecimal
impl DivAssign<&SignedDecimal> for SignedDecimal
Source§fn div_assign(&mut self, other: &SignedDecimal)
fn div_assign(&mut self, other: &SignedDecimal)
/=
operation. Read moreSource§impl DivAssign<Int128> for SignedDecimal
impl DivAssign<Int128> for SignedDecimal
Source§fn div_assign(&mut self, rhs: Int128)
fn div_assign(&mut self, rhs: Int128)
/=
operation. Read moreSource§impl DivAssign for SignedDecimal
impl DivAssign for SignedDecimal
Source§fn div_assign(&mut self, rhs: SignedDecimal)
fn div_assign(&mut self, rhs: SignedDecimal)
/=
operation. Read moreSource§impl Fraction<Int128> for SignedDecimal
impl Fraction<Int128> for SignedDecimal
Source§impl From<SignedDecimal> for SignedDecimal256
impl From<SignedDecimal> for SignedDecimal256
Source§fn from(value: SignedDecimal) -> Self
fn from(value: SignedDecimal) -> Self
Source§impl FromStr for SignedDecimal
impl FromStr for SignedDecimal
Source§fn from_str(input: &str) -> Result<Self, Self::Err>
fn from_str(input: &str) -> Result<Self, Self::Err>
Converts the decimal string to a SignedDecimal Possible inputs: “1.23”, “1”, “000012”, “1.123000000”, “-1.12300” Disallowed: “”, “.23”
This never performs any kind of rounding. More than DECIMAL_PLACES fractional digits, even zeros, result in an error.
Source§impl JsonSchema for SignedDecimal
impl JsonSchema for SignedDecimal
Source§fn schema_name() -> String
fn schema_name() -> String
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moreSource§impl Mul<&SignedDecimal> for &SignedDecimal
impl Mul<&SignedDecimal> for &SignedDecimal
Source§type Output = <SignedDecimal as Mul>::Output
type Output = <SignedDecimal as Mul>::Output
*
operator.Source§fn mul(
self,
other: &SignedDecimal,
) -> <SignedDecimal as Mul<SignedDecimal>>::Output
fn mul( self, other: &SignedDecimal, ) -> <SignedDecimal as Mul<SignedDecimal>>::Output
*
operation. Read moreSource§impl Mul<&SignedDecimal> for SignedDecimal
impl Mul<&SignedDecimal> for SignedDecimal
Source§type Output = <SignedDecimal as Mul>::Output
type Output = <SignedDecimal as Mul>::Output
*
operator.Source§fn mul(
self,
other: &SignedDecimal,
) -> <SignedDecimal as Mul<SignedDecimal>>::Output
fn mul( self, other: &SignedDecimal, ) -> <SignedDecimal as Mul<SignedDecimal>>::Output
*
operation. Read moreSource§impl<'a> Mul<SignedDecimal> for &'a SignedDecimal
impl<'a> Mul<SignedDecimal> for &'a SignedDecimal
Source§type Output = <SignedDecimal as Mul>::Output
type Output = <SignedDecimal as Mul>::Output
*
operator.Source§fn mul(
self,
other: SignedDecimal,
) -> <SignedDecimal as Mul<SignedDecimal>>::Output
fn mul( self, other: SignedDecimal, ) -> <SignedDecimal as Mul<SignedDecimal>>::Output
*
operation. Read moreSource§impl Mul for SignedDecimal
impl Mul for SignedDecimal
Source§impl MulAssign<&SignedDecimal> for SignedDecimal
impl MulAssign<&SignedDecimal> for SignedDecimal
Source§fn mul_assign(&mut self, other: &SignedDecimal)
fn mul_assign(&mut self, other: &SignedDecimal)
*=
operation. Read moreSource§impl MulAssign for SignedDecimal
impl MulAssign for SignedDecimal
Source§fn mul_assign(&mut self, rhs: SignedDecimal)
fn mul_assign(&mut self, rhs: SignedDecimal)
*=
operation. Read moreSource§impl Neg for SignedDecimal
impl Neg for SignedDecimal
Source§impl Ord for SignedDecimal
impl Ord for SignedDecimal
Source§fn cmp(&self, other: &SignedDecimal) -> Ordering
fn cmp(&self, other: &SignedDecimal) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq<&SignedDecimal> for SignedDecimal
impl PartialEq<&SignedDecimal> for SignedDecimal
Source§impl<'a> PartialEq<SignedDecimal> for &'a SignedDecimal
impl<'a> PartialEq<SignedDecimal> for &'a SignedDecimal
Source§impl PartialEq for SignedDecimal
impl PartialEq for SignedDecimal
Source§impl PartialOrd for SignedDecimal
impl PartialOrd for SignedDecimal
Source§impl Rem<&SignedDecimal> for &SignedDecimal
impl Rem<&SignedDecimal> for &SignedDecimal
Source§type Output = <SignedDecimal as Rem>::Output
type Output = <SignedDecimal as Rem>::Output
%
operator.Source§fn rem(
self,
other: &SignedDecimal,
) -> <SignedDecimal as Rem<SignedDecimal>>::Output
fn rem( self, other: &SignedDecimal, ) -> <SignedDecimal as Rem<SignedDecimal>>::Output
%
operation. Read moreSource§impl Rem<&SignedDecimal> for SignedDecimal
impl Rem<&SignedDecimal> for SignedDecimal
Source§type Output = <SignedDecimal as Rem>::Output
type Output = <SignedDecimal as Rem>::Output
%
operator.Source§fn rem(
self,
other: &SignedDecimal,
) -> <SignedDecimal as Rem<SignedDecimal>>::Output
fn rem( self, other: &SignedDecimal, ) -> <SignedDecimal as Rem<SignedDecimal>>::Output
%
operation. Read moreSource§impl<'a> Rem<SignedDecimal> for &'a SignedDecimal
impl<'a> Rem<SignedDecimal> for &'a SignedDecimal
Source§type Output = <SignedDecimal as Rem>::Output
type Output = <SignedDecimal as Rem>::Output
%
operator.Source§fn rem(
self,
other: SignedDecimal,
) -> <SignedDecimal as Rem<SignedDecimal>>::Output
fn rem( self, other: SignedDecimal, ) -> <SignedDecimal as Rem<SignedDecimal>>::Output
%
operation. Read moreSource§impl Rem for SignedDecimal
impl Rem for SignedDecimal
Source§impl RemAssign<&SignedDecimal> for SignedDecimal
impl RemAssign<&SignedDecimal> for SignedDecimal
Source§fn rem_assign(&mut self, other: &SignedDecimal)
fn rem_assign(&mut self, other: &SignedDecimal)
%=
operation. Read moreSource§impl RemAssign for SignedDecimal
impl RemAssign for SignedDecimal
Source§fn rem_assign(&mut self, rhs: SignedDecimal)
fn rem_assign(&mut self, rhs: SignedDecimal)
%=
operation. Read moreSource§impl Serialize for SignedDecimal
Serializes as a decimal string
impl Serialize for SignedDecimal
Serializes as a decimal string
Source§impl Sub<&SignedDecimal> for &SignedDecimal
impl Sub<&SignedDecimal> for &SignedDecimal
Source§type Output = <SignedDecimal as Sub>::Output
type Output = <SignedDecimal as Sub>::Output
-
operator.Source§fn sub(
self,
other: &SignedDecimal,
) -> <SignedDecimal as Sub<SignedDecimal>>::Output
fn sub( self, other: &SignedDecimal, ) -> <SignedDecimal as Sub<SignedDecimal>>::Output
-
operation. Read moreSource§impl Sub<&SignedDecimal> for SignedDecimal
impl Sub<&SignedDecimal> for SignedDecimal
Source§type Output = <SignedDecimal as Sub>::Output
type Output = <SignedDecimal as Sub>::Output
-
operator.Source§fn sub(
self,
other: &SignedDecimal,
) -> <SignedDecimal as Sub<SignedDecimal>>::Output
fn sub( self, other: &SignedDecimal, ) -> <SignedDecimal as Sub<SignedDecimal>>::Output
-
operation. Read moreSource§impl<'a> Sub<SignedDecimal> for &'a SignedDecimal
impl<'a> Sub<SignedDecimal> for &'a SignedDecimal
Source§type Output = <SignedDecimal as Sub>::Output
type Output = <SignedDecimal as Sub>::Output
-
operator.Source§fn sub(
self,
other: SignedDecimal,
) -> <SignedDecimal as Sub<SignedDecimal>>::Output
fn sub( self, other: SignedDecimal, ) -> <SignedDecimal as Sub<SignedDecimal>>::Output
-
operation. Read moreSource§impl Sub for SignedDecimal
impl Sub for SignedDecimal
Source§impl SubAssign<&SignedDecimal> for SignedDecimal
impl SubAssign<&SignedDecimal> for SignedDecimal
Source§fn sub_assign(&mut self, other: &SignedDecimal)
fn sub_assign(&mut self, other: &SignedDecimal)
-=
operation. Read moreSource§impl SubAssign for SignedDecimal
impl SubAssign for SignedDecimal
Source§fn sub_assign(&mut self, rhs: SignedDecimal)
fn sub_assign(&mut self, rhs: SignedDecimal)
-=
operation. Read more