| 1 |
# icu_decimal [](https://crates.io/crates/icu_decimal) |
| 2 |
|
| 3 |
<!-- cargo-rdme start --> |
| 4 |
|
| 5 |
Formatting basic decimal numbers. |
| 6 |
|
| 7 |
This module is published as its own crate ([`icu_decimal`](https://docs.rs/icu_decimal/latest/icu_decimal/)) |
| 8 |
and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project. |
| 9 |
|
| 10 |
Support for currencies, measurement units, and compact notation is planned. To track progress, |
| 11 |
follow [icu4x#275](https://github.com/unicode-org/icu4x/issues/275). |
| 12 |
|
| 13 |
## Examples |
| 14 |
|
| 15 |
### Format a number with Bangla digits |
| 16 |
|
| 17 |
```rust |
| 18 |
use icu::decimal::DecimalFormatter; |
| 19 |
use icu::decimal::input::Decimal; |
| 20 |
use icu::locale::locale; |
| 21 |
use writeable::assert_writeable_eq; |
| 22 |
|
| 23 |
let formatter = |
| 24 |
DecimalFormatter::try_new(locale!("bn").into(), Default::default()) |
| 25 |
.expect("locale should be present"); |
| 26 |
|
| 27 |
let decimal = Decimal::from(1000007); |
| 28 |
|
| 29 |
assert_writeable_eq!(formatter.format(&decimal), "১০,০০,০০৭"); |
| 30 |
``` |
| 31 |
|
| 32 |
### Format a number with digits after the decimal separator |
| 33 |
|
| 34 |
```rust |
| 35 |
use icu::decimal::DecimalFormatter; |
| 36 |
use icu::decimal::input::Decimal; |
| 37 |
use icu::locale::Locale; |
| 38 |
use writeable::assert_writeable_eq; |
| 39 |
|
| 40 |
let formatter = |
| 41 |
DecimalFormatter::try_new(Default::default(), Default::default()) |
| 42 |
.expect("locale should be present"); |
| 43 |
|
| 44 |
let decimal = { |
| 45 |
let mut decimal = Decimal::from(200050); |
| 46 |
decimal.multiply_pow10(-2); |
| 47 |
decimal |
| 48 |
}; |
| 49 |
|
| 50 |
assert_writeable_eq!(formatter.format(&decimal), "2,000.50"); |
| 51 |
``` |
| 52 |
|
| 53 |
### Format a number using an alternative numbering system |
| 54 |
|
| 55 |
Numbering systems specified in the `-u-nu` subtag will be followed. |
| 56 |
|
| 57 |
```rust |
| 58 |
use icu::decimal::DecimalFormatter; |
| 59 |
use icu::decimal::input::Decimal; |
| 60 |
use icu::locale::locale; |
| 61 |
use writeable::assert_writeable_eq; |
| 62 |
|
| 63 |
let formatter = DecimalFormatter::try_new( |
| 64 |
locale!("th-u-nu-thai").into(), |
| 65 |
Default::default(), |
| 66 |
) |
| 67 |
.expect("locale should be present"); |
| 68 |
|
| 69 |
let decimal = Decimal::from(1000007); |
| 70 |
|
| 71 |
assert_writeable_eq!(formatter.format(&decimal), "๑,๐๐๐,๐๐๗"); |
| 72 |
``` |
| 73 |
|
| 74 |
[`DecimalFormatter`]: DecimalFormatter |
| 75 |
|
| 76 |
<!-- cargo-rdme end --> |
| 77 |
|
| 78 |
## More Information |
| 79 |
|
| 80 |
For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). |