Skip to content

Commit

Permalink
x/text: fix scientific notation by removing extraneous spaces
Browse files Browse the repository at this point in the history
The existing implementation adds extraneous spaces around the
superscriptingExponent. This is different from what ICU4C does.
It also turns out adding those extra spaces messes with other
padding calculations.

Fixes golang/go#71428

Change-Id: Id6702390dc40c17bdd5dff2597aa8e1044f5768e
Reviewed-on: https://go-review.googlesource.com/c/text/+/644121
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Sean Liao <[email protected]>
Auto-Submit: Sean Liao <[email protected]>
Reviewed-by: Tristan Swadell <[email protected]>
  • Loading branch information
jcking authored and gopherbot committed Mar 3, 2025
1 parent b18c107 commit 221d88c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 90 deletions.
2 changes: 0 additions & 2 deletions internal/number/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,7 @@ func appendScientific(dst []byte, f *Formatter, n *Digits) (b []byte, postPre, p
exp := n.Exp - int32(n.Comma)
exponential := f.Symbol(SymExponential)
if exponential == "E" {
dst = append(dst, "\u202f"...) // NARROW NO-BREAK SPACE
dst = append(dst, f.Symbol(SymSuperscriptingExponent)...)
dst = append(dst, "\u202f"...) // NARROW NO-BREAK SPACE
dst = f.AppendDigit(dst, 1)
dst = f.AppendDigit(dst, 0)
switch {
Expand Down
90 changes: 45 additions & 45 deletions internal/number/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,63 +271,63 @@ func TestAppendDecimal(t *testing.T) {
}, {
pattern: "#E0",
test: pairs{
"0": "0\u202f×\u202f10⁰",
"1": "1\u202f×\u202f10⁰",
"123.456": "1\u202f×\u202f10²",
"0": "0×10⁰",
"1": "1×10⁰",
"123.456": "1×10²",
},
}, {
pattern: "#E+0",
test: pairs{
"0": "0\u202f×\u202f10⁺⁰",
"1000": "1\u202f×\u202f10⁺³",
"1E100": "1\u202f×\u202f10⁺¹⁰⁰",
"1E-100": "1\u202f×\u202f10⁻¹⁰⁰",
"0": "0×10⁺⁰",
"1000": "1×10⁺³",
"1E100": "1×10⁺¹⁰⁰",
"1E-100": "1×10⁻¹⁰⁰",
"NaN": "NaN",
"-Inf": "-∞",
},
}, {
pattern: "##0E00",
test: pairs{
"100": "100\u202f×\u202f10⁰⁰",
"12345": "12\u202f×\u202f10⁰³",
"123.456": "123\u202f×\u202f10⁰⁰",
"100": "100×10⁰⁰",
"12345": "12×10⁰³",
"123.456": "123×10⁰⁰",
},
}, {
pattern: "##0.###E00",
test: pairs{
"100": "100\u202f×\u202f10⁰⁰",
"12345": "12.345\u202f×\u202f10⁰³",
"123456": "123.456\u202f×\u202f10⁰³",
"123.456": "123.456\u202f×\u202f10⁰⁰",
"123.4567": "123.457\u202f×\u202f10⁰⁰",
"100": "100×10⁰⁰",
"12345": "12.345×10⁰³",
"123456": "123.456×10⁰³",
"123.456": "123.456×10⁰⁰",
"123.4567": "123.457×10⁰⁰",
},
}, {
pattern: "##0.000E00",
test: pairs{
"100": "100.000\u202f×\u202f10⁰⁰",
"12345": "12.345\u202f×\u202f10⁰³",
"123.456": "123.456\u202f×\u202f10⁰⁰",
"12.3456": "12.346\u202f×\u202f10⁰⁰",
"100": "100.000×10⁰⁰",
"12345": "12.345×10⁰³",
"123.456": "123.456×10⁰⁰",
"12.3456": "12.346×10⁰⁰",
},
}, {
pattern: "@@E0",
test: pairs{
"0": "0.0\u202f×\u202f10⁰",
"99": "9.9\u202f×\u202f10¹",
"0.99": "9.9\u202f×\u202f10⁻¹",
"0": "0.0×10⁰",
"99": "9.9×10¹",
"0.99": "9.9×10⁻¹",
},
}, {
pattern: "@###E00",
test: pairs{
"0": "0\u202f×\u202f10⁰⁰",
"1": "1\u202f×\u202f10⁰⁰",
"11": "1.1\u202f×\u202f10⁰¹",
"111": "1.11\u202f×\u202f10⁰²",
"1111": "1.111\u202f×\u202f10⁰³",
"11111": "1.111\u202f×\u202f10⁰⁴",
"0.1": "1\u202f×\u202f10⁻⁰¹",
"0.11": "1.1\u202f×\u202f10⁻⁰¹",
"0.001": "1\u202f×\u202f10⁻⁰³",
"0": "0×10⁰⁰",
"1": "1×10⁰⁰",
"11": "1.1×10⁰¹",
"111": "1.11×10⁰²",
"1111": "1.111×10⁰³",
"11111": "1.111×10⁰⁴",
"0.1": "1×10⁻⁰¹",
"0.11": "1.1×10⁻⁰¹",
"0.001": "1×10⁻⁰³",
},
}, {
pattern: "*x##0",
Expand All @@ -354,18 +354,18 @@ func TestAppendDecimal(t *testing.T) {
"1234.567": "1234.567",
},
}, {
pattern: "**0.0#######E00",
pattern: "**0.0#####E00",
test: pairs{
"0": "***0.0\u202f×\u202f10⁰⁰",
"10": "***1.0\u202f×\u202f10⁰¹",
"11": "***1.1\u202f×\u202f10⁰¹",
"111": "**1.11\u202f×\u202f10⁰²",
"1111": "*1.111\u202f×\u202f10⁰³",
"11111": "1.1111\u202f×\u202f10⁰⁴",
"11110": "*1.111\u202f×\u202f10⁰⁴",
"11100": "**1.11\u202f×\u202f10⁰⁴",
"11000": "***1.1\u202f×\u202f10⁰⁴",
"10000": "***1.0\u202f×\u202f10⁰⁴",
"0": "***0.0×10⁰⁰",
"10": "***1.0×10⁰¹",
"11": "***1.1×10⁰¹",
"111": "**1.11×10⁰²",
"1111": "*1.111×10⁰³",
"11111": "1.1111×10⁰⁴",
"11110": "*1.111×10⁰⁴",
"11100": "**1.11×10⁰⁴",
"11000": "***1.1×10⁰⁴",
"10000": "***1.0×10⁰⁴",
},
}, {
pattern: "*xpre0suf",
Expand Down Expand Up @@ -500,9 +500,9 @@ func TestFormatters(t *testing.T) {
want string
}{
{f.InitDecimal, "123456.78", "123,456.78"},
{f.InitScientific, "123456.78", "1.23\u202f×\u202f10⁵"},
{f.InitEngineering, "123456.78", "123.46\u202f×\u202f10³"},
{f.InitEngineering, "1234", "1.23\u202f×\u202f10³"},
{f.InitScientific, "123456.78", "1.23×10⁵"},
{f.InitEngineering, "123456.78", "123.46×10³"},
{f.InitEngineering, "1234", "1.23×10³"},

{f.InitPercent, "0.1234", "12.34%"},
{f.InitPerMille, "0.1234", "123.40‰"},
Expand Down
72 changes: 36 additions & 36 deletions message/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ var fmtTests = []struct {
{"%#.68U", '日', zeroFill("U+", 68, "65E5") + " '日'"},

// floats
{"%+.3e", 0.0, "+0.000\u202f×\u202f10⁰⁰"},
{"%+.3e", 1.0, "+1.000\u202f×\u202f10⁰⁰"},
{"%+.3e", 0.0, "+0.000×10⁰⁰"},
{"%+.3e", 1.0, "+1.000×10⁰⁰"},
{"%+.3f", -1.0, "-1.000"},
{"%+.3F", -1.0, "-1.000"},
{"%+.3F", float32(-1.0), "-1.000"},
Expand All @@ -436,8 +436,8 @@ var fmtTests = []struct {
{"%-+07.2f", -1.0, "-1.00 "},
{"%+10.2f", +1.0, " +1.00"},
{"%+10.2f", -1.0, " -1.00"},
{"% .3E", -1.0, "-1.000\u202f×\u202f10⁰⁰"},
{"% .3e", 1.0, " 1.000\u202f×\u202f10⁰⁰"},
{"% .3E", -1.0, "-1.000×10⁰⁰"},
{"% .3e", 1.0, " 1.000×10⁰⁰"},
{"%+.3g", 0.0, "+0"},
{"%+.3g", 1.0, "+1"},
{"%+.3g", -1.0, "-1"},
Expand Down Expand Up @@ -501,24 +501,24 @@ var fmtTests = []struct {
{"% .f", 0i, "( 0+0i)"},
{"%+.f", 0i, "(+0+0i)"},
{"% +.f", 0i, "(+0+0i)"},
{"%+.3e", 0i, "(+0.000\u202f×\u202f10⁰⁰+0.000\u202f×\u202f10⁰⁰i)"},
{"%+.3e", 0i, "(+0.000×10⁰⁰+0.000×10⁰⁰i)"},
{"%+.3f", 0i, "(+0.000+0.000i)"},
{"%+.3g", 0i, "(+0+0i)"},
{"%+.3e", 1 + 2i, "(+1.000\u202f×\u202f10⁰⁰+2.000\u202f×\u202f10⁰⁰i)"},
{"%+.3e", 1 + 2i, "(+1.000×10⁰⁰+2.000×10⁰⁰i)"},
{"%+.3f", 1 + 2i, "(+1.000+2.000i)"},
{"%+.3g", 1 + 2i, "(+1+2i)"},
{"%.3e", 0i, "(0.000\u202f×\u202f10⁰⁰+0.000\u202f×\u202f10⁰⁰i)"},
{"%.3e", 0i, "(0.000×10⁰⁰+0.000×10⁰⁰i)"},
{"%.3f", 0i, "(0.000+0.000i)"},
{"%.3F", 0i, "(0.000+0.000i)"},
{"%.3F", complex64(0i), "(0.000+0.000i)"},
{"%.3g", 0i, "(0+0i)"},
{"%.3e", 1 + 2i, "(1.000\u202f×\u202f10⁰⁰+2.000\u202f×\u202f10⁰⁰i)"},
{"%.3e", 1 + 2i, "(1.000×10⁰⁰+2.000×10⁰⁰i)"},
{"%.3f", 1 + 2i, "(1.000+2.000i)"},
{"%.3g", 1 + 2i, "(1+2i)"},
{"%.3e", -1 - 2i, "(-1.000\u202f×\u202f10⁰⁰-2.000\u202f×\u202f10⁰⁰i)"},
{"%.3e", -1 - 2i, "(-1.000×10⁰⁰-2.000×10⁰⁰i)"},
{"%.3f", -1 - 2i, "(-1.000-2.000i)"},
{"%.3g", -1 - 2i, "(-1-2i)"},
{"% .3E", -1 - 2i, "(-1.000\u202f×\u202f10⁰⁰-2.000\u202f×\u202f10⁰⁰i)"},
{"% .3E", -1 - 2i, "(-1.000×10⁰⁰-2.000×10⁰⁰i)"},
{"%+.3g", 1 + 2i, "(+1+2i)"},
{"%+.3g", complex64(1 + 2i), "(+1+2i)"},
{"%#g", 1 + 2i, "(1.00000+2.00000i)"},
Expand Down Expand Up @@ -562,42 +562,42 @@ var fmtTests = []struct {
{"%-08G", complex(NaN, NaN), "(NaN +NaN i)"},

// old test/fmt_test.go
{"%e", 1.0, "1.000000\u202f×\u202f10⁰⁰"},
{"%e", 1234.5678e3, "1.234568\u202f×\u202f10⁰⁶"},
{"%e", 1234.5678e-8, "1.234568\u202f×\u202f10⁻⁰⁵"},
{"%e", -7.0, "-7.000000\u202f×\u202f10⁰⁰"},
{"%e", -1e-9, "-1.000000\u202f×\u202f10⁻⁰⁹"},
{"%e", 1.0, "1.000000×10⁰⁰"},
{"%e", 1234.5678e3, "1.234568×10⁰⁶"},
{"%e", 1234.5678e-8, "1.234568×10⁻⁰⁵"},
{"%e", -7.0, "-7.000000×10⁰⁰"},
{"%e", -1e-9, "-1.000000×10⁻⁰⁹"},
{"%f", 1234.5678e3, "1,234,567.800000"},
{"%f", 1234.5678e-8, "0.000012"},
{"%f", -7.0, "-7.000000"},
{"%f", -1e-9, "-0.000000"},
{"%g", 1234.5678e3, "1.2345678\u202f×\u202f10⁰⁶"},
{"%g", float32(1234.5678e3), "1.2345678\u202f×\u202f10⁰⁶"},
{"%g", 1234.5678e-8, "1.2345678\u202f×\u202f10⁻⁰⁵"},
{"%g", 1234.5678e3, "1.2345678×10⁰⁶"},
{"%g", float32(1234.5678e3), "1.2345678×10⁰⁶"},
{"%g", 1234.5678e-8, "1.2345678×10⁻⁰⁵"},
{"%g", -7.0, "-7"},
{"%g", -1e-9, "-1\u202f×\u202f10⁻⁰⁹"},
{"%g", float32(-1e-9), "-1\u202f×\u202f10⁻⁰⁹"},
{"%E", 1.0, "1.000000\u202f×\u202f10⁰⁰"},
{"%E", 1234.5678e3, "1.234568\u202f×\u202f10⁰⁶"},
{"%E", 1234.5678e-8, "1.234568\u202f×\u202f10⁻⁰⁵"},
{"%E", -7.0, "-7.000000\u202f×\u202f10⁰⁰"},
{"%E", -1e-9, "-1.000000\u202f×\u202f10⁻⁰⁹"},
{"%G", 1234.5678e3, "1.2345678\u202f×\u202f10⁰⁶"},
{"%G", float32(1234.5678e3), "1.2345678\u202f×\u202f10⁰⁶"},
{"%G", 1234.5678e-8, "1.2345678\u202f×\u202f10⁻⁰⁵"},
{"%g", -1e-9, "-1×10⁻⁰⁹"},
{"%g", float32(-1e-9), "-1×10⁻⁰⁹"},
{"%E", 1.0, "1.000000×10⁰⁰"},
{"%E", 1234.5678e3, "1.234568×10⁰⁶"},
{"%E", 1234.5678e-8, "1.234568×10⁻⁰⁵"},
{"%E", -7.0, "-7.000000×10⁰⁰"},
{"%E", -1e-9, "-1.000000×10⁻⁰⁹"},
{"%G", 1234.5678e3, "1.2345678×10⁰⁶"},
{"%G", float32(1234.5678e3), "1.2345678×10⁰⁶"},
{"%G", 1234.5678e-8, "1.2345678×10⁻⁰⁵"},
{"%G", -7.0, "-7"},
{"%G", -1e-9, "-1\u202f×\u202f10⁻⁰⁹"},
{"%G", float32(-1e-9), "-1\u202f×\u202f10⁻⁰⁹"},
{"%G", -1e-9, "-1×10⁻⁰⁹"},
{"%G", float32(-1e-9), "-1×10⁻⁰⁹"},
{"%20.5s", "qwertyuiop", " qwert"},
{"%.5s", "qwertyuiop", "qwert"},
{"%-20.5s", "qwertyuiop", "qwert "},
{"%20c", 'x', " x"},
{"%-20c", 'x', "x "},
{"%20.6e", 1.2345e3, " 1.234500\u202f×\u202f10⁰³"},
{"%20.6e", 1.2345e-3, " 1.234500\u202f×\u202f10⁻⁰³"},
{"%20e", 1.2345e3, " 1.234500\u202f×\u202f10⁰³"},
{"%20e", 1.2345e-3, " 1.234500\u202f×\u202f10⁻⁰³"},
{"%20.8e", 1.2345e3, " 1.23450000\u202f×\u202f10⁰³"},
{"%20.6e", 1.2345e3, " 1.234500×10⁰³"},
{"%20.6e", 1.2345e-3, " 1.234500×10⁻⁰³"},
{"%20e", 1.2345e3, " 1.234500×10⁰³"},
{"%20e", 1.2345e-3, " 1.234500×10⁻⁰³"},
{"%20.8e", 1.2345e3, " 1.23450000×10⁰³"},
{"%20f", 1.23456789e3, " 1,234.567890"},
{"%20f", 1.23456789e-3, " 0.001235"},
{"%20f", 12345678901.23456789, "12,345,678,901.234568"},
Expand All @@ -606,7 +606,7 @@ var fmtTests = []struct {
{"%20.8f", 1.23456789e-3, " 0.00123457"},
{"%g", 1.23456789e3, "1,234.56789"},
{"%g", 1.23456789e-3, "0.00123456789"},
{"%g", 1.23456789e20, "1.23456789\u202f×\u202f10²⁰"},
{"%g", 1.23456789e20, "1.23456789×10²⁰"},

// arrays
{"%v", array, "[1 2 3 4 5]"},
Expand Down
14 changes: 7 additions & 7 deletions number/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,31 +129,31 @@ func TestFormatter(t *testing.T) {
}, {
desc: "scientific",
f: Scientific(3.00),
want: "3\u202f×\u202f10⁰",
want: "3×10⁰",
}, {
desc: "scientific",
f: Scientific(1234),
want: "1.234\u202f×\u202f10³",
want: "1.234×10³",
}, {
desc: "scientific",
f: Scientific(1234, Scale(2)),
want: "1.23\u202f×\u202f10³",
want: "1.23×10³",
}, {
desc: "engineering",
f: Engineering(12345),
want: "12.345\u202f×\u202f10³",
want: "12.345×10³",
}, {
desc: "engineering scale",
f: Engineering(12345, Scale(2)),
want: "12.34\u202f×\u202f10³",
want: "12.34×10³",
}, {
desc: "engineering precision(4)",
f: Engineering(12345, Precision(4)),
want: "12.34\u202f×\u202f10³",
want: "12.34×10³",
}, {
desc: "engineering precision(2)",
f: Engineering(1234.5, Precision(2)),
want: "1.2\u202f×\u202f10³",
want: "1.2×10³",
}, {
desc: "percent",
f: Percent(0.12),
Expand Down

0 comments on commit 221d88c

Please sign in to comment.