Skip to main content

Decimal

Struct Decimal 

Source
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

Source

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.

Source

pub fn unscaled(&self) -> i128

The integer this is counted in units of.

Source

pub fn scale(&self) -> u16

How many of the digits are after the point.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn signum(&self) -> i64

One, nought or minus one, after the sign of this number.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn sub(&self, other: &Decimal) -> Option<Decimal>

The difference of two decimals, at the larger of the two scales.

Source

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.

Source

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.

Source

pub fn rem(&self, other: &Decimal) -> Option<Decimal>

The remainder of a division, at the larger of the two scales, with the sign of the left operand as every other remainder in this engine has.

Source

pub fn negate(&self) -> Option<Decimal>

This number with its sign turned around.

Trait Implementations§

Source§

impl Clone for Decimal

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 Copy for Decimal

Source§

impl Debug for Decimal

Source§

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

Formats the value using the given formatter. Read more
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.

Source§

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

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

impl Eq for Decimal

Source§

impl Hash for Decimal

Source§

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

Hashed on the number and not on the pair, so that two values which compare equal hash alike and a decimal can key a group.

The normal form is the unscaled integer with its trailing noughts taken off, which every spelling of one number reduces to.

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 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.

Source§

fn cmp(&self, other: &Decimal) -> 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 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.

Source§

fn eq(&self, other: &Decimal) -> 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 Decimal

Source§

fn partial_cmp(&self, other: &Decimal) -> 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

Auto Trait Implementations§

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.