Skip to main content

Module humanize

Module humanize 

Source
Expand description

Human-readable template filters — intcomma, intword, naturalsize, ordinal, apnumber, naturaltime, naturalday. See humanize. Human-readable number, size and time filters for Tera.

  • Numbers: intcomma, intword, apnumber, ordinal, format_number, format_currency.
  • Byte sizes: naturalsize (1024-base), naturalsize_si (1000-base).
  • Time: naturaltime, naturaltime_short, naturalday, timesince, timeuntil, format_duration_long, format_duration_short.

Call register_filters on a Tera instance to add them all:

ⓘ
let mut tera = tera::Tera::default();
rustango::humanize::register_filters(&mut tera);
// now {{ count | intcomma }} renders "1,234,567"

Output is en-US. format_number and format_currency take a locale argument; every other filter writes English words.

Functions§

apnumber
Spell out 1 to 9 as "one" to "nine", following AP style. Any other value comes back as its digits.
format_currency
Format an amount of money: format_number plus the currency’s symbol, in the place that currency uses.
format_duration_long
Spell out a chrono::Duration, as in "2 hours, 5 minutes, 3 seconds". Empty parts are left out and a zero duration reads "0 seconds". A negative duration gets a leading minus sign.
format_duration_short
format_duration_long in compact form, as in "2h5m3s". Empty parts are left out, zero reads "0s", and a negative duration gets a leading minus sign. Use it where the long form is too wide, such as a dashboard or a table cell.
format_number
Format a number with the decimal point and thousands separator of a locale. Currency symbols live in format_currency.
intcomma
Put thousands separators into an integer. For floats use intcomma_f64.
intcomma_f64
intcomma for f64: separators go in the whole-number part, the fraction is left alone.
intword
Write a large number as a word: 1_200_000 becomes "1.2 million". Below one million the integer comes back plain.
naturalday
Name a day relative to today: "today", "yesterday", "tomorrow", or a date like "Apr 27".
naturalsize
A byte count people can read, on the 1024 scale. Under 1024 you get "N bytes", or "1 byte" for exactly one.
naturalsize_si
naturalsize on the 1000 scale instead of 1024. Use it when the number should match what a disk vendor prints on the box, or a quota expressed in decimal units.
naturaltime
A time relative to now: "3 minutes ago", "in 5 hours", or "now" within 30 seconds either way.
naturaltime_short
naturaltime in a short form for tight spaces: "45s ago", "3m ago", "4h ago", "2d ago", "3mo ago", "2y ago", or "now". A future time reads "in 10m".
ordinal
Add the English ordinal suffix: 1 becomes "1st", 2 becomes "2nd". 11, 12 and 13 always take “th”; every other number follows its last digit. A negative number takes the same suffix as its absolute value.
register_filters
Add every humanize filter to tera. Call it at app setup, right after you build the Tera instance.