-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NumFmt formats a number with a given precision using the requested decimal, grouping, and negative characters. Fixes #1444
- Loading branch information
1 parent
e92ce83
commit 93b3b13
Showing
4 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package lang | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/spf13/hugo/deps" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNumFormat(t *testing.T) { | ||
t.Parallel() | ||
|
||
ns := New(&deps.Deps{}) | ||
|
||
cases := []struct { | ||
prec int | ||
n float64 | ||
runes string | ||
|
||
want string | ||
}{ | ||
{2, -12345.6789, "", "-12,345.68"}, | ||
{2, -12345.6789, "- . ,", "-12,345.68"}, | ||
{2, -12345.1234, "- . ,", "-12,345.12"}, | ||
|
||
{2, 12345.6789, "- . ,", "12,345.68"}, | ||
{0, 12345.6789, "- . ,", "12,346"}, | ||
{11, -12345.6789, "- . ,", "-12,345.67890000000"}, | ||
|
||
{3, -12345.6789, "- ,", "-12345,679"}, | ||
{6, -12345.6789, "- , .", "-12.345,678900"}, | ||
|
||
// Arabic, ar_AE | ||
{6, -12345.6789, "- ٫ ٬", "-12٬345٫678900"}, | ||
} | ||
|
||
for i, c := range cases { | ||
errMsg := fmt.Sprintf("[%d] %v", i, c) | ||
|
||
var s string | ||
var err error | ||
|
||
if len(c.runes) == 0 { | ||
s, err = ns.NumFmt(c.prec, c.n) | ||
} else { | ||
s, err = ns.NumFmt(c.prec, c.n, c.runes) | ||
} | ||
|
||
require.NoError(t, err, errMsg) | ||
assert.Equal(t, c.want, s, errMsg) | ||
} | ||
} |