Skip to main content

PgNumeric

Struct PgNumeric 

Source
pub struct PgNumeric(/* private fields */);
Expand description

A PostgreSQL numeric, equal, ordered and hashed by value as cmp_numerics compares it, with NaN equal to itself and above Infinity.

Its Display is numeric_out, so equal values can print differently.

use sql_values::postgres::PgNumeric;

let numeric = |text| PgNumeric::parse(text).unwrap();
assert_eq!(numeric("1.50"), numeric("1.5"));
assert_eq!(numeric("1.50").to_string(), "1.50");
assert!(numeric("NaN") > numeric("Infinity"));
assert!(numeric("-inf") < numeric("-1e1000"));
assert_eq!(PgNumeric::from(-12_i64), numeric("-12.0"));

Implementations§

Source§

impl PgNumeric

Source

pub fn checked_add(&self, other: &Self) -> Result<Self, NumericError>

numeric + numeric, PostgreSQL’s numeric_add, exact at the larger display scale. NaN with anything, and opposite infinities, give NaN.

§Errors

NumericError::FormatOverflow for a sum beyond what PostgreSQL stores.

use sql_values::postgres::PgNumeric;

let numeric = |text| PgNumeric::parse(text).unwrap();
assert_eq!(numeric("1.50").checked_add(&numeric("2.250")).unwrap().to_string(), "3.750");
assert!(PgNumeric::INFINITY.checked_add(&PgNumeric::NEG_INFINITY).unwrap().is_nan());
Source

pub fn checked_sub(&self, other: &Self) -> Result<Self, NumericError>

numeric - numeric, PostgreSQL’s numeric_sub, exact at the larger display scale. NaN with anything, and like infinities, give NaN.

§Errors

NumericError::FormatOverflow for a difference beyond what PostgreSQL stores.

Source

pub fn checked_mul(&self, other: &Self) -> Result<Self, NumericError>

numeric * numeric, PostgreSQL’s numeric_mul, exact at the sum of the display scales, rounded to 16383 digits after the point beyond them. NaN with anything, and an infinity by zero, give NaN.

§Errors

NumericError::FormatOverflow for a product beyond what PostgreSQL stores.

use sql_values::postgres::{NumericError, PgNumeric};

let numeric = |text| PgNumeric::parse(text).unwrap();
assert_eq!(numeric("1.5").checked_mul(&numeric("2.25")).unwrap().to_string(), "3.375");
assert!(PgNumeric::INFINITY.checked_mul(&numeric("0")).unwrap().is_nan());
assert_eq!(
    numeric("1e131071").checked_mul(&numeric("10")),
    Err(NumericError::FormatOverflow),
);
Source

pub fn checked_div(&self, other: &Self) -> Result<Self, NumericError>

numeric / numeric, PostgreSQL’s numeric_div, rounded half away from zero to the scale select_div_scale picks, at least 16 significant digits and at least either display scale, up to 1000 digits after the point. NaN with anything, and an infinity by an infinity, give NaN, and a finite value by an infinity gives zero.

§Errors

NumericError::DivisionByZero for a divisor of zero, and NumericError::FormatOverflow for a quotient beyond what PostgreSQL stores.

use sql_values::postgres::{NumericError, PgNumeric};

let numeric = |text| PgNumeric::parse(text).unwrap();
let third = numeric("10").checked_div(&numeric("3")).unwrap();
assert_eq!(third.to_string(), "3.3333333333333333");
assert_eq!(
    PgNumeric::INFINITY.checked_div(&numeric("0")),
    Err(NumericError::DivisionByZero),
);
Source

pub fn checked_rem(&self, other: &Self) -> Result<Self, NumericError>

numeric % numeric, PostgreSQL’s numeric_mod, the remainder of the quotient truncated toward zero, with the dividend’s sign, at the larger display scale. NaN with anything, and an infinity by anything but zero, give NaN, and a finite value by an infinity is itself.

§Errors

NumericError::DivisionByZero for a divisor of zero.

use sql_values::postgres::PgNumeric;

let numeric = |text| PgNumeric::parse(text).unwrap();
assert_eq!(numeric("-7.5").checked_rem(&numeric("2")).unwrap().to_string(), "-1.5");
Source

pub fn abs(self) -> Self

abs(numeric), PostgreSQL’s numeric_abs, keeping the display scale.

Source

pub fn round(self, scale: i32) -> Result<Self, NumericError>

round(numeric, scale), PostgreSQL’s numeric_round, half away from zero to scale digits after the point, before it for a negative scale, with a display scale of scale or zero. round(numeric) is a scale of 0. NaN and the infinities are left as they are.

§Errors

NumericError::FormatOverflow where rounding up carries past what PostgreSQL stores.

use sql_values::postgres::PgNumeric;

let numeric = |text| PgNumeric::parse(text).unwrap();
assert_eq!(numeric("-2.5").round(0).unwrap().to_string(), "-3");
assert_eq!(numeric("1234.5678").round(-2).unwrap().to_string(), "1200");
assert_eq!(numeric("1.5").round(3).unwrap().to_string(), "1.500");
Source

pub fn trunc(self, scale: i32) -> Self

trunc(numeric, scale), PostgreSQL’s numeric_trunc, toward zero at scale digits after the point, before it for a negative scale, with a display scale of scale or zero. trunc(numeric) is a scale of 0. NaN and the infinities are left as they are.

use sql_values::postgres::PgNumeric;

assert_eq!(PgNumeric::parse("-1.99").unwrap().trunc(1).to_string(), "-1.9");
Source

pub fn floor(self) -> Result<Self, NumericError>

floor(numeric), PostgreSQL’s numeric_floor, the greatest whole number not above the value. NaN and the infinities are left as they are.

§Errors

NumericError::FormatOverflow where the step down carries past what PostgreSQL stores.

use sql_values::postgres::PgNumeric;

assert_eq!(PgNumeric::parse("-0.5").unwrap().floor().unwrap().to_string(), "-1");
Source

pub fn ceil(self) -> Result<Self, NumericError>

ceil(numeric), PostgreSQL’s numeric_ceil, the least whole number not below the value. NaN and the infinities are left as they are.

§Errors

NumericError::FormatOverflow where the step up carries past what PostgreSQL stores.

use sql_values::postgres::PgNumeric;

assert_eq!(PgNumeric::parse("-0.5").unwrap().ceil().unwrap().to_string(), "0");
Source§

impl PgNumeric

Source

pub const NAN: Self

NaN, equal to itself and above every other value.

Source

pub const INFINITY: Self

Infinity.

Source

pub const NEG_INFINITY: Self

-Infinity.

Source

pub fn parse(text: &str) -> Result<Self, NumericError>

PostgreSQL’s numeric_in with no type modifier, reading blanks, a sign, a decimal number with an optional exponent, a 0x, 0o or 0b integer, NaN or an infinity, then blanks.

§Errors

NumericError::InvalidInputSyntax for text numeric_in cannot read, and NumericError::FormatOverflow for a value beyond what PostgreSQL stores.

use sql_values::postgres::{NumericError, PgNumeric};

assert_eq!(PgNumeric::parse(" -1_000.50 ").unwrap().to_string(), "-1000.50");
assert_eq!(PgNumeric::parse("0x1F").unwrap().to_string(), "31");
assert_eq!(PgNumeric::parse("1e-3").unwrap().to_string(), "0.001");
assert!(PgNumeric::parse("-inf").unwrap().is_infinite());
assert_eq!(PgNumeric::parse("1e131072"), Err(NumericError::FormatOverflow));
Source

pub const fn is_nan(&self) -> bool

Whether the value is NaN.

Source

pub const fn is_infinite(&self) -> bool

Whether the value is Infinity or -Infinity.

Source

pub fn is_zero(&self) -> bool

Whether the value is zero, at any display scale.

Source

pub const fn is_negative(&self) -> bool

Whether the value is below zero, -Infinity included.

Source

pub fn is_integral(&self) -> bool

Whether the value is a finite whole number, at any display scale.

use sql_values::postgres::PgNumeric;

assert!(PgNumeric::parse("2.00").unwrap().is_integral());
assert!(!PgNumeric::parse("2.5").unwrap().is_integral());
assert!(!PgNumeric::INFINITY.is_integral());
Source

pub fn to_i64_rounded(&self) -> Option<i64>

numericvar_to_int64, the value rounded half away from zero, or None for a value beyond i64 or not finite.

Source

pub fn cmp_integer(&self, value: i64) -> Ordering

The order of the value against the numeric of an integer, without making it.

Trait Implementations§

Source§

impl Clone for PgNumeric

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PgNumeric

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for PgNumeric

numeric_out.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for PgNumeric

Source§

impl From<i16> for PgNumeric

int2_numeric.

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for PgNumeric

int4_numeric.

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for PgNumeric

int8_numeric.

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl Hash for PgNumeric

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Neg for PgNumeric

numeric_uminus, flipping the sign of every value but zero and NaN, keeping the display scale.

Source§

type Output = PgNumeric

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Ord for PgNumeric

cmp_numerics, ordering -Infinity below finite values by value, then Infinity, then NaN.

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for PgNumeric

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for PgNumeric

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<&PgNumeric> for BigDecimal

Available on crate feature decimal only.

The finite value at the scale numeric_out prints it with, so 1.50 keeps its scale of 2.

use bigdecimal::BigDecimal;
use sql_values::postgres::{NonFiniteNumeric, PgNumeric};

let numeric = |text| PgNumeric::parse(text).unwrap();
assert_eq!(BigDecimal::try_from(&numeric("1.50")).unwrap().to_string(), "1.50");
assert_eq!(BigDecimal::try_from(&numeric("NaN")), Err(NonFiniteNumeric));
Source§

type Error = NonFiniteNumeric

The type returned in the event of a conversion error.
Source§

fn try_from(numeric: &PgNumeric) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl Forget for PgNumeric

§

impl Freeze for PgNumeric
where *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl Move for PgNumeric

§

impl RefUnwindSafe for PgNumeric

§

impl Send for PgNumeric
where RawVecInner: Send,

§

impl Sync for PgNumeric
where RawVecInner: Sync,

§

impl Unpin for PgNumeric
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for PgNumeric

§

impl UnwindSafe for PgNumeric
where RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.