pub struct Money(/* private fields */);Expand description
Representation of the Postgres ‘money’ type
Implementations§
Source§impl Money
impl Money
Sourcepub fn parse_str(input: &str) -> Result<Money, Error>
pub fn parse_str(input: &str) -> Result<Money, Error>
Attempt to parse a &str into a Money.
NOTE: as of this writing, only the Postgres en_US.UTF-8 locale is supported.
For more information about the Postgres money type, please see
8.2. Monetary Types.
For more about how locales work with monetary values, please see lc_monetary.
§Examples
Parse a string of dollars
use postgres_money::Money;
let dollars = "324023040222";
let money = Money::parse_str(dollars).unwrap();
assert_eq!("$324023040222.00", money.to_string());Parse a string of cents
use postgres_money::Money;
let dollars = ".32";
let money = Money::parse_str(dollars).unwrap();
assert_eq!("$0.32", money.to_string());Parse a string with dollars and cents
use postgres_money::Money;
let dollars = "93.32";
let money = Money::parse_str(dollars).unwrap();
assert_eq!("$93.32", money.to_string());Handles parentheses
use postgres_money::Money;
let dollars = "(93.32)";
let money = Money::parse_str(dollars).unwrap();
assert_eq!("-$93.32", money.to_string());Handles dollar symbols
use postgres_money::Money;
let dollars = "$93.32";
let money = Money::parse_str(dollars).unwrap();
assert_eq!("$93.32", money.to_string());Rounds correctly
use postgres_money::Money;
let dollars = "$123.454";
let money = Money::parse_str(dollars).unwrap();
assert_eq!("$123.45", money.to_string());
let dollars = "$123.455";
let money = Money::parse_str(dollars).unwrap();
assert_eq!("$123.46", money.to_string());Handles commas
use postgres_money::Money;
let dollars = "$123,456.78";
let money = Money::parse_str(dollars).unwrap();
assert_eq!("$123456.78", money.to_string());Max value
use postgres_money::Money;
let dollars = "92233720368547758.07";
let money = Money::parse_str(dollars).unwrap();
assert_eq!("$92233720368547758.07", money.to_string());
assert_eq!(Money::max().to_string(), money.to_string());Min value
use postgres_money::Money;
let dollars = "-92233720368547758.08";
let money = Money::parse_str(dollars).unwrap();
assert_eq!("-$92233720368547758.08", money.to_string());
assert_eq!(Money::min().to_string(), money.to_string());Trait Implementations§
Source§impl<'de> Deserialize<'de> for Money
impl<'de> Deserialize<'de> for Money
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<'a> FromSql<'a> for Money
impl<'a> FromSql<'a> for Money
Source§fn from_sql(_: &Type, buf: &[u8]) -> Result<Money, Box<dyn Error + Sync + Send>>
fn from_sql(_: &Type, buf: &[u8]) -> Result<Money, Box<dyn Error + Sync + Send>>
Creates a new value of this type from a buffer of data of the specified
Postgres
Type in its binary format. Read moreSource§fn accepts(ty: &Type) -> bool
fn accepts(ty: &Type) -> bool
Determines if a value of this type can be created from the specified
Postgres
Type.Source§impl Ord for Money
impl Ord for Money
Source§impl PartialOrd for Money
impl PartialOrd for Money
Source§impl ToSql for Money
impl ToSql for Money
Source§fn to_sql(
&self,
_: &Type,
w: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn to_sql( &self, _: &Type, w: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
Converts the value of
self into the binary format of the specified
Postgres Type, appending it to out. Read moreSource§fn accepts(ty: &Type) -> bool
fn accepts(ty: &Type) -> bool
Determines if a value of this type can be converted to the specified
Postgres
Type.Source§fn to_sql_checked(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn to_sql_checked( &self, ty: &Type, out: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
An adaptor method used internally by Rust-Postgres. Read more
Source§fn encode_format(&self, _ty: &Type) -> Format
fn encode_format(&self, _ty: &Type) -> Format
Specify the encode format
impl Copy for Money
impl Eq for Money
impl StructuralPartialEq for Money
Auto Trait Implementations§
impl Freeze for Money
impl RefUnwindSafe for Money
impl Send for Money
impl Sync for Money
impl Unpin for Money
impl UnwindSafe for Money
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
Mutably borrows from an owned value. Read more
Source§impl<T> BorrowToSql for Twhere
T: ToSql,
impl<T> BorrowToSql for Twhere
T: ToSql,
Source§fn borrow_to_sql(&self) -> &dyn ToSql
fn borrow_to_sql(&self) -> &dyn ToSql
Returns a reference to
self as a ToSql trait object.