mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [formatter] only group leading digits in Comma (#419)
* fix(package): [formatter] only group leading digits in Comma * test(package): [formatter] cover Comma with exponent and suffixes
This commit is contained in:
1 parent
961a76b6d4
commit
0112b99cd9
2 files changed
+11
-3
No files matched your search
@@ -32,9 +32,11 @@ func Comma[T constraints.Float | constraints.Integer | string](value T, prefixSy
|
|||||||
numString = numString[1:]
|
numString = numString[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
index := strings.Index(numString, ".")
|
// Only the leading run of digits gets commas, so that an exponent ("1e+21")
|
||||||
if index == -1 {
|
// or any other suffix that ParseFloat accepts is left as it is.
|
||||||
index = len(numString)
|
index := 0
|
||||||
|
for index < len(numString) && numString[index] >= '0' && numString[index] <= '9' {
|
||||||
|
index++
|
||||||
}
|
}
|
||||||
|
|
||||||
for index > 3 {
|
for index > 3 {
|
||||||
|
|||||||
@@ -35,6 +35,12 @@ func TestComma(t *testing.T) {
|
|||||||
assert.Equal("-999", Comma(-999, ""))
|
assert.Equal("-999", Comma(-999, ""))
|
||||||
assert.Equal("-1,000", Comma(-1000, ""))
|
assert.Equal("-1,000", Comma(-1000, ""))
|
||||||
assert.Equal("-1,234,567", Comma(-1234567, ""))
|
assert.Equal("-1,234,567", Comma(-1234567, ""))
|
||||||
|
|
||||||
|
assert.Equal("1e+21", Comma("1e+21", ""))
|
||||||
|
assert.Equal("$12,345,678e3", Comma("12345678e3", "$"))
|
||||||
|
assert.Equal("1_000", Comma("1_000", ""))
|
||||||
|
assert.Equal("1,234,567.", Comma("1234567.", ""))
|
||||||
|
assert.Equal(".12345", Comma(".12345", ""))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPretty(t *testing.T) {
|
func TestPretty(t *testing.T) {
|
||||||
|
|||||||
Reference in new issue
Block a user