Was this page helpful?
Caution
You're viewing documentation for a deprecated version of Scylla Rust Driver. Switch to the latest stable version.
Decimal
is represented as value::CqlDecimal
or bigdecimal::BigDecimal
Without any feature flags, the user can interact with decimal
type by making use of value::CqlDecimal
which is a very simple wrapper representing the value as signed binary number in big-endian order with a 32-bit scale.
use futures::TryStreamExt;
use scylla::frame::value::CqlDecimal;
use std::str::FromStr;
// Insert a decimal (123.456) into the table
let to_insert: CqlDecimal =
CqlDecimal::from_signed_be_bytes_and_exponent(vec![0x01, 0xE2, 0x40], 3);
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read a decimal from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.into_typed::<(CqlDecimal,)>();
while let Some((decimal_value,)) = iter.try_next().await? {
println!("{:?}", decimal_value);
}
To make use of bigdecimal::Bigdecimal
type, user should enable bigdecimal-04
crate feature.
use futures::TryStreamExt;
use bigdecimal::BigDecimal;
use std::str::FromStr;
// Insert a decimal into the table
let to_insert: BigDecimal = BigDecimal::from_str("12345.0")?;
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read a decimal from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.into_typed::<(BigDecimal,)>();
while let Some((decimal_value,)) = iter.try_next().await? {
println!("{:?}", decimal_value);
}
Was this page helpful?