Struct primitives::UnifiedNum
source · pub struct UnifiedNum(_);
Expand description
Unified Number with a precision of 8 digits after the decimal point.
The number can be a maximum of u64::MAX
(the underlying type),
or in a UnifiedNum
value 184_467_440_737.09551615
.
The actual number is handled as a unsigned number and only the display shows the decimal point.
This number is (de)serialized as a Javascript number which is f64
.
As far as the numbers don’t exceed 2**63
, the Javascript number should be sufficient without losing precision
Examples
use primitives::UnifiedNum;
use serde_json::Value;
let unified_num = UnifiedNum::from(42_999_987_654_321);
// Printing the unified num will show the value and the decimal point with precision of `UnifiedNum::PRECISION` (i.e. `8`) numbers after the decimal point
assert_eq!("42999987654321", &unified_num.to_string());
assert_eq!("429999.87654321", &unified_num.to_float_string());
// Printing the Debug of unified num will show the value and the decimal point with precision of `UnifiedNum::PRECISION` (i.e. `8`) numbers after the decimal point
assert_eq!("UnifiedNum(429999.87654321)".to_string(), format!("{:?}", &unified_num));
// JSON Serializing and Deserializing the `UnifiedNum` yields a string without any decimal points
assert_eq!(Value::String("42999987654321".to_string()), serde_json::to_value(unified_num).unwrap());
Implementations
sourceimpl UnifiedNum
impl UnifiedNum
sourcepub const PRECISION: u8 = 8u8
pub const PRECISION: u8 = 8u8
The precision of the UnifiedNum
is 8 decimal numbers after the comma.
sourcepub const MULTIPLIER: u64 = 100_000_000u64
pub const MULTIPLIER: u64 = 100_000_000u64
The whole number multiplier when dealing with a UnifiedNum
.
Examples
use primitives::UnifiedNum;
let whole_number = 8_u64; // we want to represent 8.00_000_000
assert_eq!(UnifiedNum::from_u64(800_000_000), UnifiedNum::from(whole_number * UnifiedNum::MULTIPLIER));
pub const DEBUG_DELIMITER: char = '.'
pub const ZERO: UnifiedNum = _
sourcepub const ONE: UnifiedNum = _
pub const ONE: UnifiedNum = _
The whole number 1
as a UnifiedNum
.
One (1
) followed by exactly 8 zeroes (0
).
1.00_000_000
100_000_000
pub fn div_floor(&self, other: &Self) -> Self
sourcepub const fn from_u64(value: u64) -> Self
pub const fn from_u64(value: u64) -> Self
This method creates a UnifiedNum
from an inner u64
value.
This method does not take into account precision of UnifiedNum
!
sourcepub const fn to_u64(self) -> u64
pub const fn to_u64(self) -> u64
This method returns the inner u64
representation of the UnifiedNum
.
This method does not take into account precision of UnifiedNum
!
pub fn to_bignum(self) -> BigNum
pub fn checked_add(&self, rhs: &UnifiedNum) -> Option<Self>
pub fn checked_sub(&self, rhs: &UnifiedNum) -> Option<Self>
pub fn checked_mul(&self, rhs: &UnifiedNum) -> Option<Self>
pub fn checked_div(&self, rhs: &UnifiedNum) -> Option<Self>
pub fn checked_rem(&self, rhs: &UnifiedNum) -> Option<Self>
sourcepub fn to_precision(self, precision: u8) -> BigNum
pub fn to_precision(self, precision: u8) -> BigNum
Transform the UnifiedNum precision 8 to a new precision
sourcepub fn from_precision(amount: BigNum, precision: u8) -> Option<Self>
pub fn from_precision(amount: BigNum, precision: u8) -> Option<Self>
Transform the BigNum of a given precision to UnifiedNum with precision 8
If the resulting value is larger that what UnifiedNum can hold, it will return None
pub fn to_float_string(self) -> String
Trait Implementations
sourceimpl Add<&UnifiedNum> for &UnifiedNum
impl Add<&UnifiedNum> for &UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
+
operator.sourcefn add(self, rhs: &UnifiedNum) -> Self::Output
fn add(self, rhs: &UnifiedNum) -> Self::Output
+
operation. Read moresourceimpl Add<UnifiedNum> for UnifiedNum
impl Add<UnifiedNum> for UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
+
operator.sourcefn add(self, rhs: UnifiedNum) -> Self::Output
fn add(self, rhs: UnifiedNum) -> Self::Output
+
operation. Read moresourceimpl AddAssign<&UnifiedNum> for UnifiedNum
impl AddAssign<&UnifiedNum> for UnifiedNum
sourcefn add_assign(&mut self, rhs: &UnifiedNum)
fn add_assign(&mut self, rhs: &UnifiedNum)
+=
operation. Read moresourceimpl CheckedAdd for UnifiedNum
impl CheckedAdd for UnifiedNum
sourcefn checked_add(&self, v: &Self) -> Option<Self>
fn checked_add(&self, v: &Self) -> Option<Self>
None
is
returned. Read moresourceimpl CheckedDiv for UnifiedNum
impl CheckedDiv for UnifiedNum
sourcefn checked_div(&self, rhs: &Self) -> Option<Self>
fn checked_div(&self, rhs: &Self) -> Option<Self>
None
is returned. Read moresourceimpl CheckedMul for UnifiedNum
impl CheckedMul for UnifiedNum
sourcefn checked_mul(&self, v: &Self) -> Option<Self>
fn checked_mul(&self, v: &Self) -> Option<Self>
None
is returned. Read moresourceimpl CheckedRem for UnifiedNum
impl CheckedRem for UnifiedNum
sourcefn checked_rem(&self, v: &Self) -> Option<Self>
fn checked_rem(&self, v: &Self) -> Option<Self>
None
is returned. Read moresourceimpl CheckedSub for UnifiedNum
impl CheckedSub for UnifiedNum
sourcefn checked_sub(&self, v: &Self) -> Option<Self>
fn checked_sub(&self, v: &Self) -> Option<Self>
None
is returned. Read moresourceimpl Clone for UnifiedNum
impl Clone for UnifiedNum
sourcefn clone(&self) -> UnifiedNum
fn clone(&self) -> UnifiedNum
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresourceimpl Debug for UnifiedNum
impl Debug for UnifiedNum
sourceimpl Default for UnifiedNum
impl Default for UnifiedNum
sourcefn default() -> UnifiedNum
fn default() -> UnifiedNum
sourceimpl<'de> Deserialize<'de> for UnifiedNum
impl<'de> Deserialize<'de> for UnifiedNum
sourcefn 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>,
sourceimpl Display for UnifiedNum
impl Display for UnifiedNum
sourceimpl Div<&UnifiedNum> for &UnifiedNum
impl Div<&UnifiedNum> for &UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
/
operator.sourcefn div(self, rhs: &UnifiedNum) -> Self::Output
fn div(self, rhs: &UnifiedNum) -> Self::Output
/
operation. Read moresourceimpl Div<&UnifiedNum> for UnifiedNum
impl Div<&UnifiedNum> for UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
/
operator.sourcefn div(self, rhs: &UnifiedNum) -> Self::Output
fn div(self, rhs: &UnifiedNum) -> Self::Output
/
operation. Read moresourceimpl Div<UnifiedNum> for UnifiedNum
impl Div<UnifiedNum> for UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
/
operator.sourcefn div(self, rhs: UnifiedNum) -> Self::Output
fn div(self, rhs: UnifiedNum) -> Self::Output
/
operation. Read moresourceimpl From<UnifiedNum> for String
impl From<UnifiedNum> for String
sourcefn from(unified_num: UnifiedNum) -> Self
fn from(unified_num: UnifiedNum) -> Self
sourceimpl From<u64> for UnifiedNum
impl From<u64> for UnifiedNum
sourceimpl FromPrimitive for UnifiedNum
impl FromPrimitive for UnifiedNum
sourcefn from_i64(n: i64) -> Option<Self>
fn from_i64(n: i64) -> Option<Self>
i64
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_u64(n: u64) -> Option<Self>
fn from_u64(n: u64) -> Option<Self>
u64
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_isize(n: isize) -> Option<Self>
fn from_isize(n: isize) -> Option<Self>
isize
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_i8(n: i8) -> Option<Self>
fn from_i8(n: i8) -> Option<Self>
i8
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_i16(n: i16) -> Option<Self>
fn from_i16(n: i16) -> Option<Self>
i16
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_i32(n: i32) -> Option<Self>
fn from_i32(n: i32) -> Option<Self>
i32
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_i128(n: i128) -> Option<Self>
fn from_i128(n: i128) -> Option<Self>
i128
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_usize(n: usize) -> Option<Self>
fn from_usize(n: usize) -> Option<Self>
usize
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_u8(n: u8) -> Option<Self>
fn from_u8(n: u8) -> Option<Self>
u8
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_u16(n: u16) -> Option<Self>
fn from_u16(n: u16) -> Option<Self>
u16
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_u32(n: u32) -> Option<Self>
fn from_u32(n: u32) -> Option<Self>
u32
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_u128(n: u128) -> Option<Self>
fn from_u128(n: u128) -> Option<Self>
u128
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourceimpl<'a> FromSql<'a> for UnifiedNum
impl<'a> FromSql<'a> for UnifiedNum
sourcefn from_sql(
ty: &Type,
raw: &'a [u8]
) -> Result<UnifiedNum, Box<dyn Error + Sync + Send>>
fn from_sql(
ty: &Type,
raw: &'a [u8]
) -> Result<UnifiedNum, Box<dyn Error + Sync + Send>>
Type
in its binary format. Read moresourcefn accepts(ty: &Type) -> bool
fn accepts(ty: &Type) -> bool
Type
. Read moresourceimpl FromStr for UnifiedNum
impl FromStr for UnifiedNum
sourceimpl FromWhole<f64> for UnifiedNum
impl FromWhole<f64> for UnifiedNum
sourcefn from_whole(number: f64) -> Self
fn from_whole(number: f64) -> Self
sourcefn from_whole_opt(number: f64) -> Option<Self>
fn from_whole_opt(number: f64) -> Option<Self>
Self::from_whole
but instead of panicking it returns an Option.sourceimpl FromWhole<u64> for UnifiedNum
impl FromWhole<u64> for UnifiedNum
sourcefn from_whole(whole_number: u64) -> Self
fn from_whole(whole_number: u64) -> Self
sourcefn from_whole_opt(whole_number: u64) -> Option<Self>
fn from_whole_opt(whole_number: u64) -> Option<Self>
Self::from_whole
but instead of panicking it returns an Option.sourceimpl Hash for UnifiedNum
impl Hash for UnifiedNum
sourceimpl Integer for UnifiedNum
impl Integer for UnifiedNum
sourcefn is_multiple_of(&self, other: &Self) -> bool
fn is_multiple_of(&self, other: &Self) -> bool
sourcefn div_rem(&self, other: &Self) -> (Self, Self)
fn div_rem(&self, other: &Self) -> (Self, Self)
(quotient, remainder)
. Read moresourcefn gcd_lcm(&self, other: &Self) -> (Self, Self)
fn gcd_lcm(&self, other: &Self) -> (Self, Self)
sourcefn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self>where
Self: Clone,
fn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self>where
Self: Clone,
sourcefn div_mod_floor(&self, other: &Self) -> (Self, Self)
fn div_mod_floor(&self, other: &Self) -> (Self, Self)
(quotient, remainder)
. Read moresourcefn next_multiple_of(&self, other: &Self) -> Selfwhere
Self: Clone,
fn next_multiple_of(&self, other: &Self) -> Selfwhere
Self: Clone,
sourcefn prev_multiple_of(&self, other: &Self) -> Selfwhere
Self: Clone,
fn prev_multiple_of(&self, other: &Self) -> Selfwhere
Self: Clone,
sourceimpl Mul<&UnifiedNum> for &UnifiedNum
impl Mul<&UnifiedNum> for &UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
*
operator.sourcefn mul(self, rhs: &UnifiedNum) -> Self::Output
fn mul(self, rhs: &UnifiedNum) -> Self::Output
*
operation. Read moresourceimpl Mul<&UnifiedNum> for UnifiedNum
impl Mul<&UnifiedNum> for UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
*
operator.sourcefn mul(self, rhs: &UnifiedNum) -> Self::Output
fn mul(self, rhs: &UnifiedNum) -> Self::Output
*
operation. Read moresourceimpl Mul<UnifiedNum> for UnifiedNum
impl Mul<UnifiedNum> for UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
*
operator.sourcefn mul(self, rhs: UnifiedNum) -> Self::Output
fn mul(self, rhs: UnifiedNum) -> Self::Output
*
operation. Read moresourceimpl Num for UnifiedNum
impl Num for UnifiedNum
type FromStrRadixErr = <u64 as Num>::FromStrRadixErr
sourcefn from_str_radix(s: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
fn from_str_radix(s: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
2..=36
). Read moresourceimpl One for UnifiedNum
impl One for UnifiedNum
sourceimpl Ord for UnifiedNum
impl Ord for UnifiedNum
sourcefn cmp(&self, other: &UnifiedNum) -> Ordering
fn cmp(&self, other: &UnifiedNum) -> Ordering
1.21.0 · sourcefn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 · sourcefn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
sourceimpl PartialEq<UnifiedNum> for UnifiedNum
impl PartialEq<UnifiedNum> for UnifiedNum
sourcefn eq(&self, other: &UnifiedNum) -> bool
fn eq(&self, other: &UnifiedNum) -> bool
sourceimpl PartialOrd<UnifiedNum> for UnifiedNum
impl PartialOrd<UnifiedNum> for UnifiedNum
sourcefn partial_cmp(&self, other: &UnifiedNum) -> Option<Ordering>
fn partial_cmp(&self, other: &UnifiedNum) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl Rem<&UnifiedNum> for &UnifiedNum
impl Rem<&UnifiedNum> for &UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
%
operator.sourcefn rem(self, rhs: &UnifiedNum) -> Self::Output
fn rem(self, rhs: &UnifiedNum) -> Self::Output
%
operation. Read moresourceimpl Rem<UnifiedNum> for UnifiedNum
impl Rem<UnifiedNum> for UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
%
operator.sourcefn rem(self, rhs: UnifiedNum) -> Self::Output
fn rem(self, rhs: UnifiedNum) -> Self::Output
%
operation. Read moresourceimpl Serialize for UnifiedNum
impl Serialize for UnifiedNum
sourceimpl Sub<&UnifiedNum> for &UnifiedNum
impl Sub<&UnifiedNum> for &UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
-
operator.sourcefn sub(self, rhs: &UnifiedNum) -> Self::Output
fn sub(self, rhs: &UnifiedNum) -> Self::Output
-
operation. Read moresourceimpl Sub<&UnifiedNum> for UnifiedNum
impl Sub<&UnifiedNum> for UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
-
operator.sourcefn sub(self, rhs: &UnifiedNum) -> Self::Output
fn sub(self, rhs: &UnifiedNum) -> Self::Output
-
operation. Read moresourceimpl Sub<UnifiedNum> for &UnifiedNum
impl Sub<UnifiedNum> for &UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
-
operator.sourcefn sub(self, rhs: UnifiedNum) -> Self::Output
fn sub(self, rhs: UnifiedNum) -> Self::Output
-
operation. Read moresourceimpl Sub<UnifiedNum> for UnifiedNum
impl Sub<UnifiedNum> for UnifiedNum
type Output = UnifiedNum
type Output = UnifiedNum
-
operator.sourcefn sub(self, rhs: UnifiedNum) -> Self::Output
fn sub(self, rhs: UnifiedNum) -> Self::Output
-
operation. Read moresourceimpl<'a> Sum<&'a UnifiedNum> for BigNum
impl<'a> Sum<&'a UnifiedNum> for BigNum
sourceimpl<'a> Sum<&'a UnifiedNum> for Option<UnifiedNum>
impl<'a> Sum<&'a UnifiedNum> for Option<UnifiedNum>
sourcefn sum<I: Iterator<Item = &'a UnifiedNum>>(iter: I) -> Self
fn sum<I: Iterator<Item = &'a UnifiedNum>>(iter: I) -> Self
Self
from the elements by
“summing up” the items. Read moresourceimpl Sum<UnifiedNum> for Option<UnifiedNum>
impl Sum<UnifiedNum> for Option<UnifiedNum>
sourcefn sum<I: Iterator<Item = UnifiedNum>>(iter: I) -> Self
fn sum<I: Iterator<Item = UnifiedNum>>(iter: I) -> Self
Self
from the elements by
“summing up” the items. Read moresourceimpl ToPrimitive for UnifiedNum
impl ToPrimitive for UnifiedNum
sourcefn to_i64(&self) -> Option<i64>
fn to_i64(&self) -> Option<i64>
self
to an i64
. If the value cannot be
represented by an i64
, then None
is returned. Read moresourcefn to_u64(&self) -> Option<u64>
fn to_u64(&self) -> Option<u64>
self
to a u64
. If the value cannot be
represented by a u64
, then None
is returned. Read moresourcefn to_isize(&self) -> Option<isize>
fn to_isize(&self) -> Option<isize>
self
to an isize
. If the value cannot be
represented by an isize
, then None
is returned. Read moresourcefn to_i8(&self) -> Option<i8>
fn to_i8(&self) -> Option<i8>
self
to an i8
. If the value cannot be
represented by an i8
, then None
is returned. Read moresourcefn to_i16(&self) -> Option<i16>
fn to_i16(&self) -> Option<i16>
self
to an i16
. If the value cannot be
represented by an i16
, then None
is returned. Read moresourcefn to_i32(&self) -> Option<i32>
fn to_i32(&self) -> Option<i32>
self
to an i32
. If the value cannot be
represented by an i32
, then None
is returned. Read moresourcefn to_i128(&self) -> Option<i128>
fn to_i128(&self) -> Option<i128>
self
to an i128
. If the value cannot be
represented by an i128
(i64
under the default implementation), then
None
is returned. Read moresourcefn to_usize(&self) -> Option<usize>
fn to_usize(&self) -> Option<usize>
self
to a usize
. If the value cannot be
represented by a usize
, then None
is returned. Read moresourcefn to_u8(&self) -> Option<u8>
fn to_u8(&self) -> Option<u8>
self
to a u8
. If the value cannot be
represented by a u8
, then None
is returned. Read moresourcefn to_u16(&self) -> Option<u16>
fn to_u16(&self) -> Option<u16>
self
to a u16
. If the value cannot be
represented by a u16
, then None
is returned. Read moresourcefn to_u32(&self) -> Option<u32>
fn to_u32(&self) -> Option<u32>
self
to a u32
. If the value cannot be
represented by a u32
, then None
is returned. Read moresourcefn to_u128(&self) -> Option<u128>
fn to_u128(&self) -> Option<u128>
self
to a u128
. If the value cannot be
represented by a u128
(u64
under the default implementation), then
None
is returned. Read moresourceimpl ToSql for UnifiedNum
impl ToSql for UnifiedNum
sourcefn to_sql(
&self,
ty: &Type,
w: &mut BytesMut
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn to_sql(
&self,
ty: &Type,
w: &mut BytesMut
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
self
into the binary format of the specified
Postgres Type
, appending it to out
. Read moresourcefn accepts(ty: &Type) -> bool
fn accepts(ty: &Type) -> bool
Type
. Read moresourcefn to_sql_checked(
&self,
ty: &Type,
out: &mut BytesMut
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn to_sql_checked(
&self,
ty: &Type,
out: &mut BytesMut
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
sourcefn encode_format(&self, _ty: &Type) -> Format
fn encode_format(&self, _ty: &Type) -> Format
sourceimpl TryFrom<String> for UnifiedNum
impl TryFrom<String> for UnifiedNum
sourceimpl TryFrom<Value> for UnifiedNum
impl TryFrom<Value> for UnifiedNum
The UnifiedNum can be expressed in the DSL either with a String or UnifiedNum
sourceimpl Zero for UnifiedNum
impl Zero for UnifiedNum
impl Copy for UnifiedNum
impl Eq for UnifiedNum
impl StructuralEq for UnifiedNum
impl StructuralPartialEq for UnifiedNum
Auto Trait Implementations
impl RefUnwindSafe for UnifiedNum
impl Send for UnifiedNum
impl Sync for UnifiedNum
impl Unpin for UnifiedNum
impl UnwindSafe for UnifiedNum
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T> BorrowToSql for Twhere
T: ToSql,
impl<T> BorrowToSql for Twhere
T: ToSql,
sourcefn borrow_to_sql(&self) -> &dyn ToSql
fn borrow_to_sql(&self) -> &dyn ToSql
self
as a ToSql
trait object.