mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-28 23:52:04 -05:00
tpl: Add a delimiter parameter to lang.NumFmt
The original implementation of NumFmt did not take into account that the options delimiter (a space) could be a valid option. Adding a delim parameter seems like the simplest, safest, and most flexible way to solve this oversight in a backwards-compatible way. Fixes #5260
This commit is contained in:
parent
91f49c0700
commit
ce264b936c
3 changed files with 38 additions and 14 deletions
|
@ -12,7 +12,7 @@ menu:
|
||||||
docs:
|
docs:
|
||||||
parent: "functions"
|
parent: "functions"
|
||||||
toc: false
|
toc: false
|
||||||
signature: ["lang.NumFmt PRECISION NUMBER [OPTIONS]"]
|
signature: ["lang.NumFmt PRECISION NUMBER [OPTIONS [DELIMITER]]"]
|
||||||
workson: []
|
workson: []
|
||||||
hugoversion:
|
hugoversion:
|
||||||
relatedfuncs: []
|
relatedfuncs: []
|
||||||
|
@ -22,7 +22,9 @@ aliases: []
|
||||||
comments:
|
comments:
|
||||||
---
|
---
|
||||||
|
|
||||||
The default options value is `- . ,`.
|
The default options value is `- . ,`. The default delimiter within the options
|
||||||
|
value is a space. If you need to use a space as one of the options, set a
|
||||||
|
custom delimiter.
|
||||||
|
|
||||||
Numbers greater than or equal to 5 are rounded up. For example, if precision is set to `0`, `1.5` becomes `2`, and `1.4` becomes `1`.
|
Numbers greater than or equal to 5 are rounded up. For example, if precision is set to `0`, `1.5` becomes `2`, and `1.4` becomes `1`.
|
||||||
|
|
||||||
|
@ -31,5 +33,6 @@ Numbers greater than or equal to 5 are rounded up. For example, if precision is
|
||||||
{{ lang.NumFmt 2 12345.6789 "- , ." }} → 12.345,68
|
{{ lang.NumFmt 2 12345.6789 "- , ." }} → 12.345,68
|
||||||
{{ lang.NumFmt 0 -12345.6789 "- . ," }} → -12,346
|
{{ lang.NumFmt 0 -12345.6789 "- . ," }} → -12,346
|
||||||
{{ lang.NumFmt 6 -12345.6789 "- ." }} → -12345.678900
|
{{ lang.NumFmt 6 -12345.6789 "- ." }} → -12345.678900
|
||||||
|
{{ lang.NumFmt 6 -12345.6789 "-|.| " "|" }} → -1 2345.678900
|
||||||
{{ -98765.4321 | lang.NumFmt 2 }} → -98,765.43
|
{{ -98765.4321 | lang.NumFmt 2 }} → -98,765.43
|
||||||
```
|
```
|
||||||
|
|
|
@ -67,15 +67,27 @@ func (ns *Namespace) NumFmt(precision, number interface{}, options ...interface{
|
||||||
var neg, dec, grp string
|
var neg, dec, grp string
|
||||||
|
|
||||||
if len(options) == 0 {
|
if len(options) == 0 {
|
||||||
// TODO(moorereason): move to site config
|
// defaults
|
||||||
neg, dec, grp = "-", ".", ","
|
neg, dec, grp = "-", ".", ","
|
||||||
} else {
|
} else {
|
||||||
|
delim := " "
|
||||||
|
|
||||||
|
if len(options) == 2 {
|
||||||
|
// custom delimiter
|
||||||
|
s, err := cast.ToStringE(options[1])
|
||||||
|
if err != nil {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
delim = s
|
||||||
|
}
|
||||||
|
|
||||||
s, err := cast.ToStringE(options[0])
|
s, err := cast.ToStringE(options[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rs := strings.Fields(s)
|
rs := strings.Split(s, delim)
|
||||||
switch len(rs) {
|
switch len(rs) {
|
||||||
case 0:
|
case 0:
|
||||||
case 1:
|
case 1:
|
||||||
|
|
|
@ -18,22 +18,27 @@ func TestNumFormat(t *testing.T) {
|
||||||
prec int
|
prec int
|
||||||
n float64
|
n float64
|
||||||
runes string
|
runes string
|
||||||
|
delim string
|
||||||
|
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{2, -12345.6789, "", "-12,345.68"},
|
{2, -12345.6789, "", "", "-12,345.68"},
|
||||||
{2, -12345.6789, "- . ,", "-12,345.68"},
|
{2, -12345.6789, "- . ,", "", "-12,345.68"},
|
||||||
{2, -12345.1234, "- . ,", "-12,345.12"},
|
{2, -12345.1234, "- . ,", "", "-12,345.12"},
|
||||||
|
|
||||||
{2, 12345.6789, "- . ,", "12,345.68"},
|
{2, 12345.6789, "- . ,", "", "12,345.68"},
|
||||||
{0, 12345.6789, "- . ,", "12,346"},
|
{0, 12345.6789, "- . ,", "", "12,346"},
|
||||||
{11, -12345.6789, "- . ,", "-12,345.67890000000"},
|
{11, -12345.6789, "- . ,", "", "-12,345.67890000000"},
|
||||||
|
|
||||||
{3, -12345.6789, "- ,", "-12345,679"},
|
{3, -12345.6789, "- ,", "", "-12345,679"},
|
||||||
{6, -12345.6789, "- , .", "-12.345,678900"},
|
{6, -12345.6789, "- , .", "", "-12.345,678900"},
|
||||||
|
|
||||||
|
{3, -12345.6789, "-|,| ", "|", "-12 345,679"},
|
||||||
|
{6, -12345.6789, "-|,| ", "|", "-12 345,678900"},
|
||||||
|
|
||||||
// Arabic, ar_AE
|
// Arabic, ar_AE
|
||||||
{6, -12345.6789, "- ٫ ٬", "-12٬345٫678900"},
|
{6, -12345.6789, "- ٫ ٬", "", "-12٬345٫678900"},
|
||||||
|
{6, -12345.6789, "-|٫| ", "|", "-12 345٫678900"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, c := range cases {
|
for i, c := range cases {
|
||||||
|
@ -45,7 +50,11 @@ func TestNumFormat(t *testing.T) {
|
||||||
if len(c.runes) == 0 {
|
if len(c.runes) == 0 {
|
||||||
s, err = ns.NumFmt(c.prec, c.n)
|
s, err = ns.NumFmt(c.prec, c.n)
|
||||||
} else {
|
} else {
|
||||||
s, err = ns.NumFmt(c.prec, c.n, c.runes)
|
if c.delim == "" {
|
||||||
|
s, err = ns.NumFmt(c.prec, c.n, c.runes)
|
||||||
|
} else {
|
||||||
|
s, err = ns.NumFmt(c.prec, c.n, c.runes, c.delim)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, err, errMsg)
|
require.NoError(t, err, errMsg)
|
||||||
|
|
Loading…
Reference in a new issue