> module
std::decimal
arbitrary-precision decimal numbers
std::decimal provides a Decimal type backed by the rust_decimal crate, giving 96 bits of significand with a scale of 0–28.
Decimals avoid the rounding issues of floating-point arithmetic and are the native representation of .NET System.Decimal values, making them essential for interacting with CoreCLR methods that take or return decimals.
> functions
A decimal close to the float's value (0.1 gives 0.1). Err(decimal::Error::OutOfRange) if the float can't be represented, such as NaN, infinity or a value too large.
> types
Wrapper so we can derive Facet and register as a native type.
decimal.is_sign_negative() -> boolWhether the sign is negative or positive. Zero is positive.
decimal.is_sign_positive() -> boolWhether the sign is negative or positive. Zero is positive.
The nearest f64. Err(decimal::Error::OutOfRange) if the conversion fails.
A recoverable error from a `std::decimal` operation. Scripts can match the variant, e.g. `Err(decimal::Error::Parse(msg))`.
| Error::OutOfRange | The value does not fit the requested numeric type, or a float cannot be represented as a decimal. |
| Error::Parse | The string is not a valid decimal number. |
> errors
decimal::Error is a matchable enum with two variants: OutOfRange (a value doesn't fit the requested type, or a float can't be represented as a decimal) and Parse (a string isn't a valid decimal).
> examples
use std::decimal;
use std::io;
fn main() -> Result<()> {
let price = decimal::from_str("19.99")?;
let qty = decimal::from_int(3);
io::println("price = {}", price);
io::println("rounded = {}", price.round());
io::println("scale = {}", price.scale());
Ok(())
}