pub struct Decimal(/* private fields */);Expand description
An exact decimal number of arbitrary precision.
This is a well-known extension type (see ext) which
holds the decimal as validated text in the syntax of JSON numbers
(-12.50, 1e-7). The text is kept as is, so trailing zeros (the
scale) are retained. The fallback is the text as string.
When deserializing, decimals, strings, integers and floats are accepted.
Number extension values (which text formats like JSON emit for
floats) are converted exactly.
use deser::ext::Decimal;
let value: Decimal = "-12.50".parse().unwrap();
assert_eq!(value.as_str(), "-12.50");
let (mantissa, exponent) = value.to_parts();
assert_eq!((mantissa.to_string(), exponent), ("-1250".to_string(), -2));
assert_eq!(Decimal::from_parts(&mantissa, exponent), value);With the rust_decimal and bigdecimal features the decimal types of
these crates serialize as Decimal.
Implementations§
Source§impl Decimal
impl Decimal
Sourcepub fn new<S: Into<String>>(value: S) -> Result<Decimal, Error>
pub fn new<S: Into<String>>(value: S) -> Result<Decimal, Error>
Creates a decimal from its text representation.
The text has to follow the syntax of JSON numbers.
Sourcepub fn from_parts(mantissa: &BigInt, exponent: i64) -> Decimal
pub fn from_parts(mantissa: &BigInt, exponent: i64) -> Decimal
Creates a decimal from a mantissa and a base 10 exponent.
The value is mantissa * 10^exponent.
Sourcepub fn to_parts(&self) -> (BigInt, i64)
pub fn to_parts(&self) -> (BigInt, i64)
Returns the mantissa and the base 10 exponent.
The value is mantissa * 10^exponent. Exponents that do not fit
into i64 saturate.
Sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Converts the decimal into its text.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Decimal
impl<'de> Deserialize<'de> for Decimal
Source§fn deserialize_into<'out>(
out: &'out mut Option<Self>,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_into<'out>( out: &'out mut Option<Self>, state: &mut State, ) -> SinkHandle<'out, 'de>
Source§fn initial_value() -> Option<Self>
fn initial_value() -> Option<Self>
Source§fn deserialize_update<'out>(
value: &'out mut Self,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut Self, state: &mut State, ) -> SinkHandle<'out, 'de>
impl Eq for Decimal
Source§impl Serialize for Decimal
impl Serialize for Decimal
Source§fn serialize(&self, _state: &mut State) -> Result<Chunk<'_>, Error>
fn serialize(&self, _state: &mut State) -> Result<Chunk<'_>, Error>
Source§fn finish(&self, _state: &mut State) -> Result<(), Error>
fn finish(&self, _state: &mut State) -> Result<(), Error>
Source§fn is_optional(&self) -> bool
fn is_optional(&self) -> bool
Source§fn container_shape(&self) -> ContainerShape
fn container_shape(&self) -> ContainerShape
impl StructuralPartialEq for Decimal
Auto Trait Implementations§
impl Freeze for Decimal
impl RefUnwindSafe for Decimal
impl Send for Decimal
impl Sync for Decimal
impl Unpin for Decimal
impl UnsafeUnpin for Decimal
impl UnwindSafe for Decimal
Blanket Implementations§
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<T> BorrowedExtension for Twhere
T: Extension,
impl<T> BorrowedExtension for Twhere
T: Extension,
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.