mirror of
https://github.com/golang/go
synced 2024-11-12 07:10:22 -07:00
fmt: fix formatting of numbers with f.space and f.plus specified
Do not replace the sign in front of a number with a space if both f.space and f.plus are both specified for number formatting. This was already the case for integers but not for floats and complex numbers. Updates: #14543. Change-Id: I07ddeb505003db84a8a7d2c743dc19fc427a00bd Reviewed-on: https://go-review.googlesource.com/19974 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
f5ab890c18
commit
75cc05fa55
@ -278,6 +278,9 @@ var fmtTests = []struct {
|
|||||||
{"%b", 1.0, "4503599627370496p-52"},
|
{"%b", 1.0, "4503599627370496p-52"},
|
||||||
|
|
||||||
// complex values
|
// complex values
|
||||||
|
{"%.f", 0i, "(0+0i)"},
|
||||||
|
{"%+.f", 0i, "(+0+0i)"},
|
||||||
|
{"% +.f", 0i, "(+0+0i)"},
|
||||||
{"%+.3e", 0i, "(+0.000e+00+0.000e+00i)"},
|
{"%+.3e", 0i, "(+0.000e+00+0.000e+00i)"},
|
||||||
{"%+.3f", 0i, "(+0.000+0.000i)"},
|
{"%+.3f", 0i, "(+0.000+0.000i)"},
|
||||||
{"%+.3g", 0i, "(+0+0i)"},
|
{"%+.3g", 0i, "(+0+0i)"},
|
||||||
@ -384,9 +387,13 @@ var fmtTests = []struct {
|
|||||||
{"%g", 1.23456789e-3, "0.00123456789"},
|
{"%g", 1.23456789e-3, "0.00123456789"},
|
||||||
{"%g", 1.23456789e20, "1.23456789e+20"},
|
{"%g", 1.23456789e20, "1.23456789e+20"},
|
||||||
{"%20e", math.Inf(1), " +Inf"},
|
{"%20e", math.Inf(1), " +Inf"},
|
||||||
|
{"% 20f", math.Inf(1), " Inf"},
|
||||||
|
{"%+20f", math.Inf(1), " +Inf"},
|
||||||
|
{"% +20f", math.Inf(1), " +Inf"},
|
||||||
{"%-20f", math.Inf(-1), "-Inf "},
|
{"%-20f", math.Inf(-1), "-Inf "},
|
||||||
{"%20g", math.NaN(), " NaN"},
|
{"%20g", math.NaN(), " NaN"},
|
||||||
{"%+20f", math.NaN(), " +NaN"},
|
{"%+20f", math.NaN(), " +NaN"},
|
||||||
|
{"% +20f", math.NaN(), " +NaN"},
|
||||||
{"% -20f", math.NaN(), " NaN "},
|
{"% -20f", math.NaN(), " NaN "},
|
||||||
{"%+-20f", math.NaN(), "+NaN "},
|
{"%+-20f", math.NaN(), "+NaN "},
|
||||||
|
|
||||||
@ -608,14 +615,16 @@ var fmtTests = []struct {
|
|||||||
"[%7.2f]",
|
"[%7.2f]",
|
||||||
"[% 7.2f]",
|
"[% 7.2f]",
|
||||||
"[%+7.2f]",
|
"[%+7.2f]",
|
||||||
|
"[% +7.2f]",
|
||||||
"[%07.2f]",
|
"[%07.2f]",
|
||||||
"[% 07.2f]",
|
"[% 07.2f]",
|
||||||
"[%+07.2f]",
|
"[%+07.2f]",
|
||||||
|
"[% +07.2f]"
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < 9; i++) {
|
for(i = 0; i < 11; i++) {
|
||||||
printf("%s: ", format[i]);
|
printf("%s: ", format[i]);
|
||||||
printf(format[i], 1.0);
|
printf(format[i], 1.0);
|
||||||
printf(" ");
|
printf(" ");
|
||||||
@ -631,9 +640,12 @@ var fmtTests = []struct {
|
|||||||
[%7.2f]: [ 1.00] [ -1.00]
|
[%7.2f]: [ 1.00] [ -1.00]
|
||||||
[% 7.2f]: [ 1.00] [ -1.00]
|
[% 7.2f]: [ 1.00] [ -1.00]
|
||||||
[%+7.2f]: [ +1.00] [ -1.00]
|
[%+7.2f]: [ +1.00] [ -1.00]
|
||||||
|
[% +7.2f]: [ +1.00] [ -1.00]
|
||||||
[%07.2f]: [0001.00] [-001.00]
|
[%07.2f]: [0001.00] [-001.00]
|
||||||
[% 07.2f]: [ 001.00] [-001.00]
|
[% 07.2f]: [ 001.00] [-001.00]
|
||||||
[%+07.2f]: [+001.00] [-001.00]
|
[%+07.2f]: [+001.00] [-001.00]
|
||||||
|
[% +07.2f]: [+001.00] [-001.00]
|
||||||
|
|
||||||
*/
|
*/
|
||||||
{"%.2f", 1.0, "1.00"},
|
{"%.2f", 1.0, "1.00"},
|
||||||
{"%.2f", -1.0, "-1.00"},
|
{"%.2f", -1.0, "-1.00"},
|
||||||
@ -647,12 +659,16 @@ var fmtTests = []struct {
|
|||||||
{"% 7.2f", -1.0, " -1.00"},
|
{"% 7.2f", -1.0, " -1.00"},
|
||||||
{"%+7.2f", 1.0, " +1.00"},
|
{"%+7.2f", 1.0, " +1.00"},
|
||||||
{"%+7.2f", -1.0, " -1.00"},
|
{"%+7.2f", -1.0, " -1.00"},
|
||||||
|
{"% +7.2f", 1.0, " +1.00"},
|
||||||
|
{"% +7.2f", -1.0, " -1.00"},
|
||||||
{"%07.2f", 1.0, "0001.00"},
|
{"%07.2f", 1.0, "0001.00"},
|
||||||
{"%07.2f", -1.0, "-001.00"},
|
{"%07.2f", -1.0, "-001.00"},
|
||||||
{"% 07.2f", 1.0, " 001.00"},
|
{"% 07.2f", 1.0, " 001.00"},
|
||||||
{"% 07.2f", -1.0, "-001.00"},
|
{"% 07.2f", -1.0, "-001.00"},
|
||||||
{"%+07.2f", 1.0, "+001.00"},
|
{"%+07.2f", 1.0, "+001.00"},
|
||||||
{"%+07.2f", -1.0, "-001.00"},
|
{"%+07.2f", -1.0, "-001.00"},
|
||||||
|
{"% +07.2f", 1.0, "+001.00"},
|
||||||
|
{"% +07.2f", -1.0, "-001.00"},
|
||||||
|
|
||||||
// Complex numbers: exhaustively tested in TestComplexFormatting.
|
// Complex numbers: exhaustively tested in TestComplexFormatting.
|
||||||
{"%7.2f", 1 + 2i, "( 1.00 +2.00i)"},
|
{"%7.2f", 1 + 2i, "( 1.00 +2.00i)"},
|
||||||
|
@ -390,8 +390,9 @@ func (f *fmt) formatFloat(v float64, verb byte, prec, n int) {
|
|||||||
} else {
|
} else {
|
||||||
num[0] = '+'
|
num[0] = '+'
|
||||||
}
|
}
|
||||||
// f.space says to replace a leading + with a space.
|
// f.space means to add a leading space instead of a "+" sign unless
|
||||||
if f.space && num[0] == '+' {
|
// the sign is explicitly asked for by f.plus.
|
||||||
|
if f.space && num[0] == '+' && !f.plus {
|
||||||
num[0] = ' '
|
num[0] = ' '
|
||||||
}
|
}
|
||||||
// Special handling for infinities and NaN,
|
// Special handling for infinities and NaN,
|
||||||
|
Loading…
Reference in New Issue
Block a user