Skip to content

Commit

Permalink
Release 0.9.0
Browse files Browse the repository at this point in the history
fpdec@0.9.0

Generated by cargo-workspaces
  • Loading branch information
mamrhein committed Dec 12, 2023
1 parent e94c669 commit e53d99b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.TXT
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Version Changes
-------- --------------------------------------------------------------------
0.9.0 Added conversion of Decimal values to primitive ints and floats.

0.8.0 Added optional support for zero-copy serialization via 'rkyv' (thx
to den-mentiei).

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fpdec"
version = "0.8.1"
version = "0.9.0"
edition = "2021"
authors = ["Michael Amrhein <michael@adrhinum.de>"]
description = "Decimal fixed-point arithmetic."
Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Add `fpdec` to your `Cargo.toml`:

```toml
[dependencies]
fpdec = "0.8"
fpdec = "0.9"
```

## Usage
Expand Down Expand Up @@ -152,6 +152,36 @@ let z: Decimal = x.div_rounded(y, 3);
assert_eq!(z.to_string(), "2.705");
```

A `Decimal` value can be converted into a float, maybe rounded to the nearest
value representable by the target type:

```rust
# use fpdec::{Dec, Decimal};
let d = Dec!(-33820900478.195);
let f = f64::from(d);
assert_eq!(f, -33820900478.19499969482421875_f64);
let f = f32::from(Dec!(0.6));
assert_eq!(f, 0.60000002384185791015625_f32);
```

Converting a `Decimal` value to a primitive int is more intricate. It is only
supported by try_from / try_into and only giving a value of the target type,
if the given value represents an integral value fitting the range of values of
the target type.

```rust
# use fpdec::{Dec, Decimal, TryFromDecimalError};
let d = Dec!(3.7);
let res = i32::try_from(d);
assert!(res.is_err());
assert_eq!(res.unwrap_err(), TryFromDecimalError::NotAnIntValue);
let d = Decimal::MAX;
let res = i128::try_from(d);
assert_eq!(res.unwrap(), i128::MAX);
let res = i64::try_from(d);
assert!(res.is_err());
assert_eq!(res.unwrap_err(), TryFromDecimalError::ValueOutOfRange);
```
## Crate features

By default, only the feature `std` is enabled.
Expand Down

0 comments on commit e53d99b

Please sign in to comment.