Skip to content

Latest commit

 

History

History
37 lines (28 loc) · 998 Bytes

commafy-thousands.md

File metadata and controls

37 lines (28 loc) · 998 Bytes
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.
commafy
thousands
separate_with_commas

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:

See Cargo.toml for the dependencies

{% include file="examples/commafy-thousands/Cargo.toml" %}

See the code:

{% include file="examples/commafy-thousands/src/main.rs" %}

See the output

     12345     12,345
       123        123
    -12345    -12,345
      -123       -123
   -1234.5   -1,234.5