Struct json::number::Number
[−]
[src]
pub struct Number { /* fields omitted */ }
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);Run
More often than not you will deal with JsonValue::Number
variant that
wraps around this type, instead of using the methods here directly.
Methods
impl Number
[src]
fn from_parts(positive: bool, mantissa: u64, exponent: i16) -> Self
Construct a new Number
from parts. This can't create a NaN value.
let pi = Number::from_parts(true, 3141592653589793, -15); assert_eq!(pi, 3.141592653589793);Run
fn as_parts(&self) -> (bool, u64, i16)
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);Run
fn is_sign_positive(&self) -> bool
fn is_zero(&self) -> bool
fn is_nan(&self) -> bool
fn is_empty(&self) -> bool
Test if the number is NaN or has a zero value.
fn as_fixed_point_u64(&self, point: u16) -> Option<u64>
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));Run
fn as_fixed_point_i64(&self, point: u16) -> Option<i64>
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));Run
Trait Implementations
impl PartialEq<JsonValue> for Number
[src]
fn eq(&self, other: &JsonValue) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl Copy for Number
[src]
impl Clone for Number
[src]
fn clone(&self) -> Number
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl Debug for Number
[src]
impl PartialEq for Number
[src]
fn eq(&self, other: &Number) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl Display for Number
[src]
impl From<f64> for Number
[src]
impl From<f32> for Number
[src]
impl PartialEq<f64> for Number
[src]
fn eq(&self, other: &f64) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl PartialEq<f32> for Number
[src]
fn eq(&self, other: &f32) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl From<isize> for Number
[src]
impl PartialEq<isize> for Number
[src]
fn eq(&self, other: &isize) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl From<i8> for Number
[src]
impl PartialEq<i8> for Number
[src]
fn eq(&self, other: &i8) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl From<i16> for Number
[src]
impl PartialEq<i16> for Number
[src]
fn eq(&self, other: &i16) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl From<i32> for Number
[src]
impl PartialEq<i32> for Number
[src]
fn eq(&self, other: &i32) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl From<i64> for Number
[src]
impl PartialEq<i64> for Number
[src]
fn eq(&self, other: &i64) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl From<usize> for Number
[src]
impl PartialEq<usize> for Number
[src]
fn eq(&self, other: &usize) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl From<u8> for Number
[src]
impl PartialEq<u8> for Number
[src]
fn eq(&self, other: &u8) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl From<u16> for Number
[src]
impl PartialEq<u16> for Number
[src]
fn eq(&self, other: &u16) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl From<u32> for Number
[src]
impl PartialEq<u32> for Number
[src]
fn eq(&self, other: &u32) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl From<u64> for Number
[src]
impl PartialEq<u64> for Number
[src]
fn eq(&self, other: &u64) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.