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
impl PgNumeric
Sourcepub fn checked_add(&self, other: &Self) -> Result<Self, NumericError>
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());Sourcepub fn checked_sub(&self, other: &Self) -> Result<Self, NumericError>
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.
Sourcepub fn checked_mul(&self, other: &Self) -> Result<Self, NumericError>
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),
);Sourcepub fn checked_div(&self, other: &Self) -> Result<Self, NumericError>
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),
);Sourcepub fn checked_rem(&self, other: &Self) -> Result<Self, NumericError>
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");Sourcepub fn round(self, scale: i32) -> Result<Self, NumericError>
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");Sourcepub fn trunc(self, scale: i32) -> Self
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");Sourcepub fn floor(self) -> Result<Self, NumericError>
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");Sourcepub fn ceil(self) -> Result<Self, NumericError>
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
impl PgNumeric
Sourcepub const NEG_INFINITY: Self
pub const NEG_INFINITY: Self
-Infinity.
Sourcepub fn parse(text: &str) -> Result<Self, NumericError>
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));Sourcepub const fn is_infinite(&self) -> bool
pub const fn is_infinite(&self) -> bool
Whether the value is Infinity or -Infinity.
Sourcepub const fn is_negative(&self) -> bool
pub const fn is_negative(&self) -> bool
Whether the value is below zero, -Infinity included.
Sourcepub fn is_integral(&self) -> bool
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());Sourcepub fn to_i64_rounded(&self) -> Option<i64>
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.
Sourcepub fn cmp_integer(&self, value: i64) -> Ordering
pub fn cmp_integer(&self, value: i64) -> Ordering
The order of the value against the numeric of an integer, without making it.
Trait Implementations§
impl Eq for PgNumeric
Source§impl Neg for PgNumeric
numeric_uminus, flipping the sign of every value but zero and NaN, keeping the display
scale.
impl Neg for PgNumeric
numeric_uminus, flipping the sign of every value but zero and NaN, keeping the display
scale.
Source§impl Ord for PgNumeric
cmp_numerics, ordering -Infinity below finite values by value, then Infinity, then NaN.
impl Ord for PgNumeric
cmp_numerics, ordering -Infinity below finite values by value, then Infinity, then NaN.
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for PgNumeric
impl PartialOrd for PgNumeric
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.
impl TryFrom<&PgNumeric> for BigDecimal
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));