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
A Decimal number implementation written in pure Rust suitable for financial calculations that require significant integral and fractional digits with no round-off errors.
15
+
A Decimal number implementation written in pure Rust suitable for financial calculations that require significant
16
+
integral and fractional digits with no round-off errors.
11
17
12
-
The binary representation consists of a 96 bit integer number, a scaling factor used to specify the decimal fraction and a 1 bit sign. Because of this representation, trailing zeros are preserved and may be exposed when in string form. These can be truncated using the `normalize` or `round_dp` functions.
18
+
The binary representation consists of a 96 bit integer number, a scaling factor used to specify the decimal fraction and
19
+
a 1 bit sign. Because of this representation, trailing zeros are preserved and may be exposed when in string form. These
20
+
can be truncated using the `normalize` or `round_dp` functions.
13
21
14
22
## Installing
15
23
@@ -33,7 +41,8 @@ rust_decimal_macros = "1.35"
33
41
34
42
## Usage
35
43
36
-
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:
44
+
Decimal numbers can be created in a few distinct ways. The easiest and most efficient method of creating a Decimal is to
45
+
use the procedural macro that can be enabled using the `macros` feature:
37
46
38
47
```rust
39
48
// Import the `rust_decimal_macros` crate and use the macro directly from there.
@@ -44,7 +53,8 @@ assert_eq!(number, dec!(2.22));
44
53
assert_eq!(number.to_string(), "2.22");
45
54
```
46
55
47
-
Alternatively you can also use one of the Decimal number convenience functions ([see the docs](https://docs.rs/rust_decimal/) for more details):
56
+
Alternatively you can also use one of the Decimal number convenience
57
+
functions ([see the docs](https://docs.rs/rust_decimal/) for more details):
48
58
49
59
```rust
50
60
// Using the prelude can help importing trait based functions (e.g. core::str::FromStr).
@@ -140,31 +150,30 @@ Enables the tokio postgres module allowing for async communication with PostgreS
140
150
141
151
### `db-diesel-postgres`
142
152
143
-
Enable `diesel` PostgreSQL support. By default, this enables version `1.4` of `diesel`. If you wish to use the `2.0`
144
-
version of `diesel` then you can do so by using the feature `db-diesel2-postgres`. Please note, if both features are
This feature isn't typically required for serialization however can be useful for deserialization purposes since it does not require
308
+
This feature isn't typically required for serialization however can be useful for deserialization purposes since it does
309
+
not require
287
310
a type hint. Consequently, you can force this for just deserialization by:
311
+
288
312
```rust
289
313
#[derive(Serialize, Deserialize)]
290
314
pubstructStrExample {
@@ -295,7 +319,8 @@ pub struct StrExample {
295
319
296
320
### `serde-with-arbitrary-precision`
297
321
298
-
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:
322
+
Enable this to access the module for deserializing `Decimal` types using the `serde_json/arbitrary_precision` feature.
An unexpected consequence of this feature is that it will serialize as a float like number. To prevent this, you can
341
+
An unexpected consequence of this feature is that it will serialize as a float like number. To prevent this, you can
316
342
target the struct to only deserialize with the `arbitrary_precision` feature:
343
+
317
344
```rust
318
345
#[derive(Serialize, Deserialize)]
319
346
pubstructArbitraryExample {
@@ -328,7 +355,8 @@ Please see the `examples` directory for more information regarding `serde_json`
328
355
329
356
### `std`
330
357
331
-
Enable `std` library support. This is enabled by default, however in the future will be opt in. For now, to support `no_std`
358
+
Enable `std` library support. This is enabled by default, however in the future will be opt in. For now, to
359
+
support `no_std`
332
360
libraries, this crate can be compiled with `--no-default-features`.
333
361
334
362
## Building
@@ -337,31 +365,41 @@ Please refer to the [Build document](BUILD.md) for more information on building
337
365
338
366
## Minimum Rust Compiler Version
339
367
340
-
The current _minimum_ compiler version is [`1.60.0`](https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1600-2022-04-07)
368
+
The current _minimum_ compiler version
369
+
is [`1.60.0`](https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1600-2022-04-07)
341
370
which was released on `2022-04-07`.
342
371
343
-
This library maintains support for rust compiler versions that are 4 minor versions away from the current stable rust compiler version.
344
-
For example, if the current stable compiler version is `1.50.0` then we will guarantee support up to and including `1.46.0`.
372
+
This library maintains support for rust compiler versions that are 4 minor versions away from the current stable rust
373
+
compiler version.
374
+
For example, if the current stable compiler version is `1.50.0` then we will guarantee support up to and
375
+
including `1.46.0`.
345
376
Of note, we will only update the minimum supported version if and when required.
346
377
347
378
## Comparison to other Decimal implementations
348
379
349
-
During the development of this library, there were various design decisions made to ensure that decimal calculations would
350
-
be quick, accurate and efficient. Some decisions, however, put limitations on what this library can do and ultimately what
351
-
it is suitable for. One such decision was the structure of the internal decimal representation.
352
-
353
-
This library uses a mantissa of 96 bits made up of three 32-bit unsigned integers with a fourth 32-bit unsigned integer to represent the scale/sign
354
-
(similar to the C and .NET Decimal implementations).
355
-
This structure allows us to make use of algorithmic optimizations to implement basic arithmetic; ultimately this gives us the ability
356
-
to squeeze out performance and make it one of the fastest implementations available. The downside of this approach however is that
380
+
During the development of this library, there were various design decisions made to ensure that decimal calculations
381
+
would
382
+
be quick, accurate and efficient. Some decisions, however, put limitations on what this library can do and ultimately
383
+
what
384
+
it is suitable for. One such decision was the structure of the internal decimal representation.
385
+
386
+
This library uses a mantissa of 96 bits made up of three 32-bit unsigned integers with a fourth 32-bit unsigned integer
387
+
to represent the scale/sign
388
+
(similar to the C and .NET Decimal implementations).
389
+
This structure allows us to make use of algorithmic optimizations to implement basic arithmetic; ultimately this gives
390
+
us the ability
391
+
to squeeze out performance and make it one of the fastest implementations available. The downside of this approach
392
+
however is that
357
393
the maximum number of significant digits that can be represented is roughly 28 base-10 digits (29 in some cases).
358
394
359
-
While this constraint is not an issue for many applications (e.g. when dealing with money), some applications may require a higher number of significant digits to be represented. Fortunately,
395
+
While this constraint is not an issue for many applications (e.g. when dealing with money), some applications may
396
+
require a higher number of significant digits to be represented. Fortunately,
360
397
there are alternative implementations that may be worth investigating, such as:
0 commit comments