HUGO
Menu
GitHub 90622 stars Mastodon

lang.FormatPercent

Returns a percentage representation of a number with the given precision for the current language and region.
Syntax
lang.FormatPercent PRECISION NUMBER
Returns
string

Usage

The lang.FormatPercent function returns a percentage representation of NUMBER, rounded to PRECISION decimal places, and formatted for the current language and region. The function treats NUMBER as a percentage, not as a fraction, so 0.5 becomes 0.5%, not 50%.

Hugo localizes the result using the locale of the current language. If the locale is not set or not recognized, Hugo uses the language key, then the defaultContentLanguage, then en. Hugo does not warn you when it falls back to another value.

Hugo does not recognize some common language tags, such as zh-CN and zh-TW. Use the equivalent tag that includes a script subtag, such as zh-Hans-CN or zh-Hant-TW.

Examples

With the locale set to en-US:

{{ lang.FormatPercent 2 512.5032 }} → 512.50%
{{ lang.FormatPercent 0 25 }} → 25%
{{ lang.FormatPercent 1 0.5 }} → 0.5%

With the locale set to de-DE:

{{ lang.FormatPercent 2 512.5032 }} → 512,50 %
{{ lang.FormatPercent 0 25 }} → 25 %
{{ lang.FormatPercent 1 0.5 }} → 0,5 %