pub struct Number { /* fields omitted */ }
Expand description
Number representation used inside JsonValue
. You can easily convert
the Number
type into native Rust number types and back, or use the
equality operator with another number type.
let foo: Number = 3.14.into();
let bar: f64 = foo.into();
assert_eq!(foo, 3.14);
assert_eq!(bar, 3.14);
More often than not you will deal with JsonValue::Number
variant that
wraps around this type, instead of using the methods here directly.
Construct a new Number
from parts. This can’t create a NaN value.
let pi = unsafe { Number::from_parts_unchecked(true, 3141592653589793, -15) };
assert_eq!(pi, 3.141592653589793);
While this method is marked unsafe, it doesn’t actually perform any unsafe operations.
THe goal of the ‘unsafe’ is to deter from using this method in favor of its safe equivalent
from_parts
, at least in context when the associated performance cost is negligible.
Construct a new Number
from parts, stripping unnecessary trailing zeroes.
This can’t create a NaN value.
let one = Number::from_parts(true, 1000, -3);
let (positive, mantissa, exponent) = one.as_parts();
assert_eq!(true, positive);
assert_eq!(1, mantissa);
assert_eq!(0, exponent);
Reverse to from_parts
- obtain parts from an existing Number
.
let pi = Number::from(3.141592653589793);
let (positive, mantissa, exponent) = pi.as_parts();
assert_eq!(positive, true);
assert_eq!(mantissa, 3141592653589793);
assert_eq!(exponent, -15);
Test if the number is NaN or has a zero value.
Obtain an integer at a fixed decimal point. This is useful for
converting monetary values and doing arithmetic on them without
rounding errors introduced by floating point operations.
Will return None
if Number
is negative or a NaN.
let price_a = Number::from(5.99);
let price_b = Number::from(7);
let price_c = Number::from(10.2);
assert_eq!(price_a.as_fixed_point_u64(2), Some(599));
assert_eq!(price_b.as_fixed_point_u64(2), Some(700));
assert_eq!(price_c.as_fixed_point_u64(2), Some(1020));
Analog to as_fixed_point_u64
, except returning a signed
i64
, properly handling negative numbers.
let balance_a = Number::from(-1.49);
let balance_b = Number::from(42);
assert_eq!(balance_a.as_fixed_point_i64(2), Some(-149));
assert_eq!(balance_b.as_fixed_point_i64(2), Some(4200));
Performs copy-assignment from source
. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
The resulting type after applying the -
operator.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
impl<T> Any for T where
T: 'static + ?Sized,
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
impl<T, U> Into<U> for T where
U: From<T>,
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String
. Read more
The type returned in the event of a conversion error.
The type returned in the event of a conversion error.