title | timestamp | author | published | description | tags | |||
---|---|---|---|---|---|---|---|---|
Commanfy using the thousands crate |
2024-03-29 09:00:01 -0700 |
szabgab |
true |
Put a comma after every 3 digits from the right-hand side to make the numbers more readable. |
|
A while ago I wrote an example showing how to commafy a number, that is how to make numbers more readable by separating every 3 digits? e.g. Instead of 1234567 it is much more readable to have this: 1,234,567.
I was pointed to the thousands crate that provides a trait called separate_with_commas
that does exactly this.
Here is how to use it:
{% include file="examples/commafy-thousands/Cargo.toml" %}
{% include file="examples/commafy-thousands/src/main.rs" %}
12345 12,345
123 123
-12345 -12,345
-123 -123
-1234.5 -1,234.5