You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+52-15Lines changed: 52 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -19,26 +19,27 @@ Using [`cargo-edit`](https://crates.io/crates/cargo-edit):
19
19
$ cargo add rust_decimal
20
20
```
21
21
22
-
In addition, if you would like to use the optimized macro for convenient creation of decimals:
22
+
If you would like to use the optimized macro for convenient creation of decimals you can add `rust_decimal` with the `macros` feature flag:
23
23
24
24
```sh
25
-
$ cargo add rust_decimal_macros
25
+
$ cargo add rust_decimal --features macros
26
26
```
27
27
28
28
Alternatively, you can edit your `Cargo.toml` directly and run `cargo update`:
29
29
30
30
```toml
31
31
[dependencies]
32
-
rust_decimal = "1.33"
33
-
rust_decimal_macros = "1.33"
32
+
rust_decimal = { version = "1.33", features = ["macros"] }
34
33
```
35
34
36
35
## Usage
37
36
38
-
Decimal numbers can be created in a few distinct ways. The easiest and most efficient method of creating a Decimal is to use the procedural macro within the `rust_decimal_macros` crate:
37
+
Decimal numbers can be created in a few distinct ways. The easiest and most efficient method of creating a Decimal is to use the procedural macro that can be enabled using the `macros` feature:
39
38
40
39
```rust
41
-
// Procedural macros need importing directly
40
+
// The macros feature exposes a `dec` macro which will parse the input into a raw decimal number at compile time.
41
+
// It is also exposed when using `rust_decimal::prelude::*`. That said, you can also import the
42
+
// `rust_decimal_macros` crate and use the macro directly from there.
42
43
userust_decimal_macros::dec;
43
44
44
45
letnumber=dec!(-1.23) +dec!(3.45);
@@ -198,8 +199,8 @@ Enable `rust-fuzz` support by implementing the `Arbitrary` trait.
198
199
199
200
### `serde-float`
200
201
201
-
**Note:**it is recommended to use the `serde-with-*` features for greater control. This allows configurability at the data
202
-
level.
202
+
> **Note:**This feature applies float serialization/deserialization rules as the default method for handling `Decimal` numbers.
203
+
See also the `serde-with-*` features for greater flexibility.
203
204
204
205
Enable this so that JSON serialization of `Decimal` types are sent as a float instead of a string (default).
205
206
@@ -212,8 +213,8 @@ e.g. with this turned on, JSON serialization would output:
212
213
213
214
### `serde-str`
214
215
215
-
**Note:**it is recommended to use the `serde-with-*` features for greater control. This allows configurability at the data
216
-
level.
216
+
> **Note:**This feature applies string serialization/deserialization rules as the default method for handling `Decimal` numbers.
217
+
See also the `serde-with-*` features for greater flexibility.
217
218
218
219
This is typically useful for `bincode` or `csv` like implementations.
219
220
@@ -227,17 +228,20 @@ converting to `f64` _loses_ precision, it's highly recommended that you do NOT e
227
228
228
229
### `serde-arbitrary-precision`
229
230
230
-
**Note:**it is recommended to use the `serde-with-*` features for greater control. This allows configurability at the data
231
-
level.
231
+
> **Note:**This feature applies arbitrary serialization/deserialization rules as the default method for handling `Decimal` numbers.
232
+
See also the `serde-with-*` features for greater flexibility.
232
233
233
234
This is used primarily with `serde_json` and consequently adds it as a "weak dependency". This supports the
234
235
`arbitrary_precision` feature inside `serde_json` when parsing decimals.
235
236
236
237
This is recommended when parsing "float" looking data as it will prevent data loss.
237
238
239
+
Please note, this currently serializes numbers in a float like format by default, which can be an unexpected consequence. For greater
240
+
control over the serialization format, please use the `serde-with-arbitrary-precision` feature.
241
+
238
242
### `serde-with-float`
239
243
240
-
Enable this to access the module for serializing `Decimal` types to a float. This can be use in `struct` definitions like so:
244
+
Enable this to access the module for serializing `Decimal` types to a float. This can be used in `struct` definitions like so:
Enable this to access the module for serializing`Decimal` types to a `String`. This can be use in `struct` definitions like so:
301
+
Enable this to access the module for deserializing`Decimal` types using the `serde_json/arbitrary_precision` feature. This can be used in `struct` definitions like so:
0 commit comments