Skip to content

Commit 221d88c

Browse files
jckinggopherbot
authored andcommitted
x/text: fix scientific notation by removing extraneous spaces
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Sean Liao <sean@liao.dev> Auto-Submit: Sean Liao <sean@liao.dev> Reviewed-by: Tristan Swadell <tswadell@google.com>
1 parent b18c107 commit 221d88c

File tree

4 files changed

+88
-90
lines changed

4 files changed

+88
-90
lines changed

internal/number/format.go

-2
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,7 @@ func appendScientific(dst []byte, f *Formatter, n *Digits) (b []byte, postPre, p
394394
exp := n.Exp - int32(n.Comma)
395395
exponential := f.Symbol(SymExponential)
396396
if exponential == "E" {
397-
dst = append(dst, "\u202f"...) // NARROW NO-BREAK SPACE
398397
dst = append(dst, f.Symbol(SymSuperscriptingExponent)...)
399-
dst = append(dst, "\u202f"...) // NARROW NO-BREAK SPACE
400398
dst = f.AppendDigit(dst, 1)
401399
dst = f.AppendDigit(dst, 0)
402400
switch {

internal/number/format_test.go

+45-45
Original file line numberDiff line numberDiff line change
@@ -271,63 +271,63 @@ func TestAppendDecimal(t *testing.T) {
271271
}, {
272272
pattern: "#E0",
273273
test: pairs{
274-
"0": "0\u202f×\u202f10⁰",
275-
"1": "1\u202f×\u202f10⁰",
276-
"123.456": "1\u202f×\u202f10²",
274+
"0": "0×10⁰",
275+
"1": "1×10⁰",
276+
"123.456": "1×10²",
277277
},
278278
}, {
279279
pattern: "#E+0",
280280
test: pairs{
281-
"0": "0\u202f×\u202f10⁺⁰",
282-
"1000": "1\u202f×\u202f10⁺³",
283-
"1E100": "1\u202f×\u202f10⁺¹⁰⁰",
284-
"1E-100": "1\u202f×\u202f10⁻¹⁰⁰",
281+
"0": "0×10⁺⁰",
282+
"1000": "1×10⁺³",
283+
"1E100": "1×10⁺¹⁰⁰",
284+
"1E-100": "1×10⁻¹⁰⁰",
285285
"NaN": "NaN",
286286
"-Inf": "-∞",
287287
},
288288
}, {
289289
pattern: "##0E00",
290290
test: pairs{
291-
"100": "100\u202f×\u202f10⁰⁰",
292-
"12345": "12\u202f×\u202f10⁰³",
293-
"123.456": "123\u202f×\u202f10⁰⁰",
291+
"100": "100×10⁰⁰",
292+
"12345": "12×10⁰³",
293+
"123.456": "123×10⁰⁰",
294294
},
295295
}, {
296296
pattern: "##0.###E00",
297297
test: pairs{
298-
"100": "100\u202f×\u202f10⁰⁰",
299-
"12345": "12.345\u202f×\u202f10⁰³",
300-
"123456": "123.456\u202f×\u202f10⁰³",
301-
"123.456": "123.456\u202f×\u202f10⁰⁰",
302-
"123.4567": "123.457\u202f×\u202f10⁰⁰",
298+
"100": "100×10⁰⁰",
299+
"12345": "12.345×10⁰³",
300+
"123456": "123.456×10⁰³",
301+
"123.456": "123.456×10⁰⁰",
302+
"123.4567": "123.457×10⁰⁰",
303303
},
304304
}, {
305305
pattern: "##0.000E00",
306306
test: pairs{
307-
"100": "100.000\u202f×\u202f10⁰⁰",
308-
"12345": "12.345\u202f×\u202f10⁰³",
309-
"123.456": "123.456\u202f×\u202f10⁰⁰",
310-
"12.3456": "12.346\u202f×\u202f10⁰⁰",
307+
"100": "100.000×10⁰⁰",
308+
"12345": "12.345×10⁰³",
309+
"123.456": "123.456×10⁰⁰",
310+
"12.3456": "12.346×10⁰⁰",
311311
},
312312
}, {
313313
pattern: "@@E0",
314314
test: pairs{
315-
"0": "0.0\u202f×\u202f10⁰",
316-
"99": "9.9\u202f×\u202f10¹",
317-
"0.99": "9.9\u202f×\u202f10⁻¹",
315+
"0": "0.0×10⁰",
316+
"99": "9.9×10¹",
317+
"0.99": "9.9×10⁻¹",
318318
},
319319
}, {
320320
pattern: "@###E00",
321321
test: pairs{
322-
"0": "0\u202f×\u202f10⁰⁰",
323-
"1": "1\u202f×\u202f10⁰⁰",
324-
"11": "1.1\u202f×\u202f10⁰¹",
325-
"111": "1.11\u202f×\u202f10⁰²",
326-
"1111": "1.111\u202f×\u202f10⁰³",
327-
"11111": "1.111\u202f×\u202f10⁰⁴",
328-
"0.1": "1\u202f×\u202f10⁻⁰¹",
329-
"0.11": "1.1\u202f×\u202f10⁻⁰¹",
330-
"0.001": "1\u202f×\u202f10⁻⁰³",
322+
"0": "0×10⁰⁰",
323+
"1": "1×10⁰⁰",
324+
"11": "1.1×10⁰¹",
325+
"111": "1.11×10⁰²",
326+
"1111": "1.111×10⁰³",
327+
"11111": "1.111×10⁰⁴",
328+
"0.1": "1×10⁻⁰¹",
329+
"0.11": "1.1×10⁻⁰¹",
330+
"0.001": "1×10⁻⁰³",
331331
},
332332
}, {
333333
pattern: "*x##0",
@@ -354,18 +354,18 @@ func TestAppendDecimal(t *testing.T) {
354354
"1234.567": "1234.567",
355355
},
356356
}, {
357-
pattern: "**0.0#######E00",
357+
pattern: "**0.0#####E00",
358358
test: pairs{
359-
"0": "***0.0\u202f×\u202f10⁰⁰",
360-
"10": "***1.0\u202f×\u202f10⁰¹",
361-
"11": "***1.1\u202f×\u202f10⁰¹",
362-
"111": "**1.11\u202f×\u202f10⁰²",
363-
"1111": "*1.111\u202f×\u202f10⁰³",
364-
"11111": "1.1111\u202f×\u202f10⁰⁴",
365-
"11110": "*1.111\u202f×\u202f10⁰⁴",
366-
"11100": "**1.11\u202f×\u202f10⁰⁴",
367-
"11000": "***1.1\u202f×\u202f10⁰⁴",
368-
"10000": "***1.0\u202f×\u202f10⁰⁴",
359+
"0": "***0.0×10⁰⁰",
360+
"10": "***1.0×10⁰¹",
361+
"11": "***1.1×10⁰¹",
362+
"111": "**1.11×10⁰²",
363+
"1111": "*1.111×10⁰³",
364+
"11111": "1.1111×10⁰⁴",
365+
"11110": "*1.111×10⁰⁴",
366+
"11100": "**1.11×10⁰⁴",
367+
"11000": "***1.1×10⁰⁴",
368+
"10000": "***1.0×10⁰⁴",
369369
},
370370
}, {
371371
pattern: "*xpre0suf",
@@ -500,9 +500,9 @@ func TestFormatters(t *testing.T) {
500500
want string
501501
}{
502502
{f.InitDecimal, "123456.78", "123,456.78"},
503-
{f.InitScientific, "123456.78", "1.23\u202f×\u202f10⁵"},
504-
{f.InitEngineering, "123456.78", "123.46\u202f×\u202f10³"},
505-
{f.InitEngineering, "1234", "1.23\u202f×\u202f10³"},
503+
{f.InitScientific, "123456.78", "1.23×10⁵"},
504+
{f.InitEngineering, "123456.78", "123.46×10³"},
505+
{f.InitEngineering, "1234", "1.23×10³"},
506506

507507
{f.InitPercent, "0.1234", "12.34%"},
508508
{f.InitPerMille, "0.1234", "123.40‰"},

message/fmt_test.go

+36-36
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ var fmtTests = []struct {
421421
{"%#.68U", '日', zeroFill("U+", 68, "65E5") + " '日'"},
422422

423423
// floats
424-
{"%+.3e", 0.0, "+0.000\u202f×\u202f10⁰⁰"},
425-
{"%+.3e", 1.0, "+1.000\u202f×\u202f10⁰⁰"},
424+
{"%+.3e", 0.0, "+0.000×10⁰⁰"},
425+
{"%+.3e", 1.0, "+1.000×10⁰⁰"},
426426
{"%+.3f", -1.0, "-1.000"},
427427
{"%+.3F", -1.0, "-1.000"},
428428
{"%+.3F", float32(-1.0), "-1.000"},
@@ -436,8 +436,8 @@ var fmtTests = []struct {
436436
{"%-+07.2f", -1.0, "-1.00 "},
437437
{"%+10.2f", +1.0, " +1.00"},
438438
{"%+10.2f", -1.0, " -1.00"},
439-
{"% .3E", -1.0, "-1.000\u202f×\u202f10⁰⁰"},
440-
{"% .3e", 1.0, " 1.000\u202f×\u202f10⁰⁰"},
439+
{"% .3E", -1.0, "-1.000×10⁰⁰"},
440+
{"% .3e", 1.0, " 1.000×10⁰⁰"},
441441
{"%+.3g", 0.0, "+0"},
442442
{"%+.3g", 1.0, "+1"},
443443
{"%+.3g", -1.0, "-1"},
@@ -501,24 +501,24 @@ var fmtTests = []struct {
501501
{"% .f", 0i, "( 0+0i)"},
502502
{"%+.f", 0i, "(+0+0i)"},
503503
{"% +.f", 0i, "(+0+0i)"},
504-
{"%+.3e", 0i, "(+0.000\u202f×\u202f10⁰⁰+0.000\u202f×\u202f10⁰⁰i)"},
504+
{"%+.3e", 0i, "(+0.000×10⁰⁰+0.000×10⁰⁰i)"},
505505
{"%+.3f", 0i, "(+0.000+0.000i)"},
506506
{"%+.3g", 0i, "(+0+0i)"},
507-
{"%+.3e", 1 + 2i, "(+1.000\u202f×\u202f10⁰⁰+2.000\u202f×\u202f10⁰⁰i)"},
507+
{"%+.3e", 1 + 2i, "(+1.000×10⁰⁰+2.000×10⁰⁰i)"},
508508
{"%+.3f", 1 + 2i, "(+1.000+2.000i)"},
509509
{"%+.3g", 1 + 2i, "(+1+2i)"},
510-
{"%.3e", 0i, "(0.000\u202f×\u202f10⁰⁰+0.000\u202f×\u202f10⁰⁰i)"},
510+
{"%.3e", 0i, "(0.000×10⁰⁰+0.000×10⁰⁰i)"},
511511
{"%.3f", 0i, "(0.000+0.000i)"},
512512
{"%.3F", 0i, "(0.000+0.000i)"},
513513
{"%.3F", complex64(0i), "(0.000+0.000i)"},
514514
{"%.3g", 0i, "(0+0i)"},
515-
{"%.3e", 1 + 2i, "(1.000\u202f×\u202f10⁰⁰+2.000\u202f×\u202f10⁰⁰i)"},
515+
{"%.3e", 1 + 2i, "(1.000×10⁰⁰+2.000×10⁰⁰i)"},
516516
{"%.3f", 1 + 2i, "(1.000+2.000i)"},
517517
{"%.3g", 1 + 2i, "(1+2i)"},
518-
{"%.3e", -1 - 2i, "(-1.000\u202f×\u202f10⁰⁰-2.000\u202f×\u202f10⁰⁰i)"},
518+
{"%.3e", -1 - 2i, "(-1.000×10⁰⁰-2.000×10⁰⁰i)"},
519519
{"%.3f", -1 - 2i, "(-1.000-2.000i)"},
520520
{"%.3g", -1 - 2i, "(-1-2i)"},
521-
{"% .3E", -1 - 2i, "(-1.000\u202f×\u202f10⁰⁰-2.000\u202f×\u202f10⁰⁰i)"},
521+
{"% .3E", -1 - 2i, "(-1.000×10⁰⁰-2.000×10⁰⁰i)"},
522522
{"%+.3g", 1 + 2i, "(+1+2i)"},
523523
{"%+.3g", complex64(1 + 2i), "(+1+2i)"},
524524
{"%#g", 1 + 2i, "(1.00000+2.00000i)"},
@@ -562,42 +562,42 @@ var fmtTests = []struct {
562562
{"%-08G", complex(NaN, NaN), "(NaN +NaN i)"},
563563

564564
// old test/fmt_test.go
565-
{"%e", 1.0, "1.000000\u202f×\u202f10⁰⁰"},
566-
{"%e", 1234.5678e3, "1.234568\u202f×\u202f10⁰⁶"},
567-
{"%e", 1234.5678e-8, "1.234568\u202f×\u202f10⁻⁰⁵"},
568-
{"%e", -7.0, "-7.000000\u202f×\u202f10⁰⁰"},
569-
{"%e", -1e-9, "-1.000000\u202f×\u202f10⁻⁰⁹"},
565+
{"%e", 1.0, "1.000000×10⁰⁰"},
566+
{"%e", 1234.5678e3, "1.234568×10⁰⁶"},
567+
{"%e", 1234.5678e-8, "1.234568×10⁻⁰⁵"},
568+
{"%e", -7.0, "-7.000000×10⁰⁰"},
569+
{"%e", -1e-9, "-1.000000×10⁻⁰⁹"},
570570
{"%f", 1234.5678e3, "1,234,567.800000"},
571571
{"%f", 1234.5678e-8, "0.000012"},
572572
{"%f", -7.0, "-7.000000"},
573573
{"%f", -1e-9, "-0.000000"},
574-
{"%g", 1234.5678e3, "1.2345678\u202f×\u202f10⁰⁶"},
575-
{"%g", float32(1234.5678e3), "1.2345678\u202f×\u202f10⁰⁶"},
576-
{"%g", 1234.5678e-8, "1.2345678\u202f×\u202f10⁻⁰⁵"},
574+
{"%g", 1234.5678e3, "1.2345678×10⁰⁶"},
575+
{"%g", float32(1234.5678e3), "1.2345678×10⁰⁶"},
576+
{"%g", 1234.5678e-8, "1.2345678×10⁻⁰⁵"},
577577
{"%g", -7.0, "-7"},
578-
{"%g", -1e-9, "-1\u202f×\u202f10⁻⁰⁹"},
579-
{"%g", float32(-1e-9), "-1\u202f×\u202f10⁻⁰⁹"},
580-
{"%E", 1.0, "1.000000\u202f×\u202f10⁰⁰"},
581-
{"%E", 1234.5678e3, "1.234568\u202f×\u202f10⁰⁶"},
582-
{"%E", 1234.5678e-8, "1.234568\u202f×\u202f10⁻⁰⁵"},
583-
{"%E", -7.0, "-7.000000\u202f×\u202f10⁰⁰"},
584-
{"%E", -1e-9, "-1.000000\u202f×\u202f10⁻⁰⁹"},
585-
{"%G", 1234.5678e3, "1.2345678\u202f×\u202f10⁰⁶"},
586-
{"%G", float32(1234.5678e3), "1.2345678\u202f×\u202f10⁰⁶"},
587-
{"%G", 1234.5678e-8, "1.2345678\u202f×\u202f10⁻⁰⁵"},
578+
{"%g", -1e-9, "-1×10⁻⁰⁹"},
579+
{"%g", float32(-1e-9), "-1×10⁻⁰⁹"},
580+
{"%E", 1.0, "1.000000×10⁰⁰"},
581+
{"%E", 1234.5678e3, "1.234568×10⁰⁶"},
582+
{"%E", 1234.5678e-8, "1.234568×10⁻⁰⁵"},
583+
{"%E", -7.0, "-7.000000×10⁰⁰"},
584+
{"%E", -1e-9, "-1.000000×10⁻⁰⁹"},
585+
{"%G", 1234.5678e3, "1.2345678×10⁰⁶"},
586+
{"%G", float32(1234.5678e3), "1.2345678×10⁰⁶"},
587+
{"%G", 1234.5678e-8, "1.2345678×10⁻⁰⁵"},
588588
{"%G", -7.0, "-7"},
589-
{"%G", -1e-9, "-1\u202f×\u202f10⁻⁰⁹"},
590-
{"%G", float32(-1e-9), "-1\u202f×\u202f10⁻⁰⁹"},
589+
{"%G", -1e-9, "-1×10⁻⁰⁹"},
590+
{"%G", float32(-1e-9), "-1×10⁻⁰⁹"},
591591
{"%20.5s", "qwertyuiop", " qwert"},
592592
{"%.5s", "qwertyuiop", "qwert"},
593593
{"%-20.5s", "qwertyuiop", "qwert "},
594594
{"%20c", 'x', " x"},
595595
{"%-20c", 'x', "x "},
596-
{"%20.6e", 1.2345e3, " 1.234500\u202f×\u202f10⁰³"},
597-
{"%20.6e", 1.2345e-3, " 1.234500\u202f×\u202f10⁻⁰³"},
598-
{"%20e", 1.2345e3, " 1.234500\u202f×\u202f10⁰³"},
599-
{"%20e", 1.2345e-3, " 1.234500\u202f×\u202f10⁻⁰³"},
600-
{"%20.8e", 1.2345e3, " 1.23450000\u202f×\u202f10⁰³"},
596+
{"%20.6e", 1.2345e3, " 1.234500×10⁰³"},
597+
{"%20.6e", 1.2345e-3, " 1.234500×10⁻⁰³"},
598+
{"%20e", 1.2345e3, " 1.234500×10⁰³"},
599+
{"%20e", 1.2345e-3, " 1.234500×10⁻⁰³"},
600+
{"%20.8e", 1.2345e3, " 1.23450000×10⁰³"},
601601
{"%20f", 1.23456789e3, " 1,234.567890"},
602602
{"%20f", 1.23456789e-3, " 0.001235"},
603603
{"%20f", 12345678901.23456789, "12,345,678,901.234568"},
@@ -606,7 +606,7 @@ var fmtTests = []struct {
606606
{"%20.8f", 1.23456789e-3, " 0.00123457"},
607607
{"%g", 1.23456789e3, "1,234.56789"},
608608
{"%g", 1.23456789e-3, "0.00123456789"},
609-
{"%g", 1.23456789e20, "1.23456789\u202f×\u202f10²⁰"},
609+
{"%g", 1.23456789e20, "1.23456789×10²⁰"},
610610

611611
// arrays
612612
{"%v", array, "[1 2 3 4 5]"},

number/number_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,31 @@ func TestFormatter(t *testing.T) {
129129
}, {
130130
desc: "scientific",
131131
f: Scientific(3.00),
132-
want: "3\u202f×\u202f10⁰",
132+
want: "3×10⁰",
133133
}, {
134134
desc: "scientific",
135135
f: Scientific(1234),
136-
want: "1.234\u202f×\u202f10³",
136+
want: "1.234×10³",
137137
}, {
138138
desc: "scientific",
139139
f: Scientific(1234, Scale(2)),
140-
want: "1.23\u202f×\u202f10³",
140+
want: "1.23×10³",
141141
}, {
142142
desc: "engineering",
143143
f: Engineering(12345),
144-
want: "12.345\u202f×\u202f10³",
144+
want: "12.345×10³",
145145
}, {
146146
desc: "engineering scale",
147147
f: Engineering(12345, Scale(2)),
148-
want: "12.34\u202f×\u202f10³",
148+
want: "12.34×10³",
149149
}, {
150150
desc: "engineering precision(4)",
151151
f: Engineering(12345, Precision(4)),
152-
want: "12.34\u202f×\u202f10³",
152+
want: "12.34×10³",
153153
}, {
154154
desc: "engineering precision(2)",
155155
f: Engineering(1234.5, Precision(2)),
156-
want: "1.2\u202f×\u202f10³",
156+
want: "1.2×10³",
157157
}, {
158158
desc: "percent",
159159
f: Percent(0.12),

0 commit comments

Comments
 (0)