HUGO
Menu
GitHub 90622 stars Mastodon

lang.FormatNumber

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

Usage

The lang.FormatNumber function returns a numeric representation of NUMBER, rounded to PRECISION decimal places, and formatted for the current language and region.

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.

To format a number with the minus sign, decimal separator, and grouping separator of your choice, use the lang.FormatNumberCustom function instead.

Examples

With the locale set to en-US:

{{ lang.FormatNumber 2 512.5032 }} → 512.50
{{ lang.FormatNumber 0 1234567.891 }} → 1,234,568
{{ lang.FormatNumber 2 -1234.5 }} → -1,234.50

With the locale set to de-DE:

{{ lang.FormatNumber 2 512.5032 }} → 512,50
{{ lang.FormatNumber 0 1234567.891 }} → 1.234.568
{{ lang.FormatNumber 2 -1234.5 }} → -1.234,50