pub struct PgMoney(pub i64);
postgres
only.Expand description
The PostgreSQL MONEY
type stores a currency amount with a fixed fractional
precision. The fractional precision is determined by the database’s
lc_monetary
setting.
Data is read and written as 64-bit signed integers, and conversion into a decimal should be done using the right precision.
Reading MONEY
value in text format is not supported and will cause an error.
§locale_frac_digits
This parameter corresponds to the number of digits after the decimal separator.
This value must match what Postgres is expecting for the locale set in the database
or else the decimal value you see on the client side will not match the money
value
on the server side.
For most locales, this value is 2
.
If you’re not sure what locale your database is set to or how many decimal digits it specifies,
you can execute SHOW lc_monetary;
to get the locale name, and then look it up in this list
(you can ignore the .utf8
prefix):
https://lh.2xlibre.net/values/frac_digits/
If that link is dead and you’re on a POSIX-compliant system (Unix, FreeBSD) you can also execute:
$ LC_MONETARY=<value returned by `SHOW lc_monetary`> locale -k frac_digits
And the value you want is N
in frac_digits=N
. If you have shell access to the database
server you should execute it there as available locales may differ between machines.
Note that if frac_digits
for the locale is outside the range [0, 10]
, Postgres assumes
it’s a sentinel value and defaults to 2:
https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/cash.c#L114-L123
Tuple Fields§
§0: i64
The raw integer value sent over the wire; for locales with frac_digits=2
(i.e. most
of them), this will be the value in whole cents.
E.g. for select '$123.45'::money
with a locale of en_US
(frac_digits=2
),
this will be 12345
.
If the currency of your locale does not have fractional units, e.g. Yen, then this will just be the units of the currency.
See the type-level docs for an explanation of locale_frac_units
.
Implementations§
source§impl PgMoney
impl PgMoney
sourcepub fn to_bigdecimal(self, locale_frac_digits: i64) -> BigDecimal
pub fn to_bigdecimal(self, locale_frac_digits: i64) -> BigDecimal
Convert the money value into a BigDecimal
using locale_frac_digits
.
See the type-level docs for an explanation of locale_frac_digits
.
sourcepub fn to_decimal(self, locale_frac_digits: u32) -> Decimal
pub fn to_decimal(self, locale_frac_digits: u32) -> Decimal
Convert the money value into a Decimal
using locale_frac_digits
.
See the type-level docs for an explanation of locale_frac_digits
.
sourcepub fn from_decimal(decimal: Decimal, locale_frac_digits: u32) -> PgMoney
pub fn from_decimal(decimal: Decimal, locale_frac_digits: u32) -> PgMoney
Convert a Decimal
value into money using locale_frac_digits
.
See the type-level docs for an explanation of locale_frac_digits
.
Note that Decimal
has 96 bits of precision, but PgMoney
only has 63 plus the sign bit.
If the value is larger than 63 bits it will be truncated.
sourcepub fn from_bigdecimal(
decimal: BigDecimal,
locale_frac_digits: u32,
) -> Result<PgMoney, Box<dyn Error + Sync + Send>>
pub fn from_bigdecimal( decimal: BigDecimal, locale_frac_digits: u32, ) -> Result<PgMoney, Box<dyn Error + Sync + Send>>
Convert a BigDecimal
value into money using the correct precision
defined in the PostgreSQL settings. The default precision is two.
Trait Implementations§
source§impl Encode<'_, Postgres> for PgMoney
impl Encode<'_, Postgres> for PgMoney
source§fn encode_by_ref(
&self,
buf: &mut PgArgumentBuffer,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn encode_by_ref( &self, buf: &mut PgArgumentBuffer, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
source§fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
self
into buf
in the expected format for the database.fn produces(&self) -> Option<<DB as Database>::TypeInfo>
fn size_hint(&self) -> usize
source§impl PgHasArrayType for PgMoney
impl PgHasArrayType for PgMoney
fn array_type_info() -> PgTypeInfo
fn array_compatible(ty: &PgTypeInfo) -> bool
impl Copy for PgMoney
impl Eq for PgMoney
impl StructuralPartialEq for PgMoney
Auto Trait Implementations§
impl Freeze for PgMoney
impl RefUnwindSafe for PgMoney
impl Send for PgMoney
impl Sync for PgMoney
impl Unpin for PgMoney
impl UnwindSafe for PgMoney
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more