pub struct Decimal { /* private fields */ }Expand description
An exact decimal: unscaled units, each one ten to the minus
scale.
The scale rides on the value rather than only on the declared type,
because a decimal exists outside any column. CAST('1.20' AS DECIMAL(5,2)) in a RETURN has no column to ask, and it still has
to print two places.
Implementations§
Source§impl Decimal
impl Decimal
Sourcepub fn new(unscaled: i128, scale: u16) -> Decimal
pub fn new(unscaled: i128, scale: u16) -> Decimal
A decimal of unscaled units of ten to the minus scale.
Nothing is normalised. The caller wrote a scale and it is kept,
so a value cast to DECIMAL(5,2) prints two places even when
the second one is a nought.
Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Whether this is exactly zero, which the sign of the unscaled integer does not answer on its own since zero has none.
Sourcepub fn digits(&self) -> u16
pub fn digits(&self) -> u16
How many digits the unscaled integer is written with, which is the precision a value needs rather than the one it was declared with. Zero is one digit.
Sourcepub fn rescale(&self, scale: u16) -> Option<Decimal>
pub fn rescale(&self, scale: u16) -> Option<Decimal>
This number written at scale instead, or None when the
change would not be exact or would not fit.
Going to a larger scale multiplies and can overflow; going to a smaller one divides and can drop a digit that is not a nought. Both are refused rather than rounded, because the caller that wants rounding says so and the caller that does not is asking whether this number is one of that type at all.
Sourcepub fn abs(&self) -> Option<Decimal>
pub fn abs(&self) -> Option<Decimal>
This number without its sign, at the scale it was written with.
None only for the one number an i128 cannot negate.
Sourcepub fn round(&self, digits: i64) -> Option<Decimal>
pub fn round(&self, digits: i64) -> Option<Decimal>
This number rounded to digits places after the point, half
away from zero, which is what ROUND means over exact numbers.
digits may be negative, which rounds to a place left of the
point: a hundred and fifty at minus two digits is two hundred.
The answer’s scale is digits where that is a scale, and nought
where it is not, because a number rounded to hundreds has no
digits after the point to keep.
Sourcepub fn ceil(&self, digits: i64) -> Option<Decimal>
pub fn ceil(&self, digits: i64) -> Option<Decimal>
This number taken up to the next digits place, toward positive
infinity, which is what CEIL means: -1.5 ceils to -1.
Sourcepub fn floor(&self, digits: i64) -> Option<Decimal>
pub fn floor(&self, digits: i64) -> Option<Decimal>
This number taken down to the previous digits place, toward
negative infinity: -1.5 floors to -2.
Sourcepub fn truncate(&self) -> Option<i128>
pub fn truncate(&self) -> Option<i128>
The whole part of this number, with the digits past the point
dropped rather than rounded, so 1.9 is 1 and -1.9 is -1.
Truncation toward zero and not rounding, because that is what the standard’s cast to an exact integer does and the caller that wants the nearest integer asks for it.
Sourcepub fn to_f64(&self) -> f64
pub fn to_f64(&self) -> f64
This number as binary floating point, which is a narrowing and is why it is spelled out rather than done wherever a float is wanted.
Every decimal has a nearest binary64 and most have no exact one, so this is the edge where exactness ends. Callers reach it when a decimal meets a float in arithmetic, where the answer is inexact whatever is done, and when a function is defined on binary floating point and on nothing else.
Sourcepub fn parse(text: &str, scale: u16) -> Option<Decimal>
pub fn parse(text: &str, scale: u16) -> Option<Decimal>
The text read as a decimal at scale, rounded half away from
zero past the scale. None is text that is not a number, or a
number too large for the carrier.
Half away from zero is what a person writing money means by
rounding, and it is what the standard’s CAST to an exact
numeric permits an implementation to choose. An exponent is
accepted because 1E3 is a number a query may write, and it is
applied to the scale rather than to a float, so nothing goes
through binary on the way.
Sourcepub fn add(&self, other: &Decimal) -> Option<Decimal>
pub fn add(&self, other: &Decimal) -> Option<Decimal>
The sum of two decimals, at the larger of the two scales, or
None on overflow.
The wider scale is the exact one: a hundredth plus a tenth is a number of hundredths, and answering in tenths would have to throw a digit away.
Sourcepub fn sub(&self, other: &Decimal) -> Option<Decimal>
pub fn sub(&self, other: &Decimal) -> Option<Decimal>
The difference of two decimals, at the larger of the two scales.
Sourcepub fn mul(&self, other: &Decimal) -> Option<Decimal>
pub fn mul(&self, other: &Decimal) -> Option<Decimal>
The product of two decimals, whose scale is the sum of the two scales, since that is what multiplying the units does.
Sourcepub fn div(&self, other: &Decimal) -> Option<Decimal>
pub fn div(&self, other: &Decimal) -> Option<Decimal>
The quotient of two decimals, or None for a division by zero
or an overflow.
A quotient is the one operation exact arithmetic cannot always answer exactly: a third has no decimal spelling at any scale. So the answer is carried to a scale that keeps what the operands knew and no further, and the last digit is rounded half away from zero. The scale chosen is the larger operand scale plus six guard digits, capped at the carrier, which is enough that money divided by a count still says what a person would write.
Trait Implementations§
impl Copy for Decimal
Source§impl Display for Decimal
Written the way it was declared: the digits with a point put in at
the scale, and no exponent, since an exact number is written out.
impl Display for Decimal
Written the way it was declared: the digits with a point put in at the scale, and no exponent, since an exact number is written out.
impl Eq for Decimal
Source§impl Hash for Decimal
impl Hash for Decimal
Source§impl Ord for Decimal
A total order, which two exact numbers always have.
impl Ord for Decimal
A total order, which two exact numbers always have.
Where the scales cannot be aligned without overflowing, the numbers are compared by sign and then by how many digits they have before the point, which orders them correctly because a number with more whole digits is the larger one.
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 PartialEq for Decimal
Two decimals are equal when they are the same number, whatever scale
each was written at, so 1.20 equals 1.2.
impl PartialEq for Decimal
Two decimals are equal when they are the same number, whatever scale
each was written at, so 1.20 equals 1.2.
The scale is how precisely a number is known and not part of which
number it is. A query grouping prices would otherwise put 1.5 and
1.50 in two groups, which is a distinction no user of a ledger
means to draw.