Skip to content

Commit d3ed04d

Browse files
committed
Fix d3.format with explicit "-" sign.
The default behaviour is to only use a minus sign for negative numbers. However, when this behaviour was explicitly specified using "-", this caused positive numbers to become negative. Fixes d3#2072.
1 parent 4e5deb0 commit d3ed04d

4 files changed

Lines changed: 15 additions & 5 deletions

File tree

d3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,7 +2133,7 @@
21332133
return t.reverse().join(locale_thousands);
21342134
} : d3_identity;
21352135
return function(specifier) {
2136-
var match = d3_format_re.exec(specifier), fill = match[1] || " ", align = match[2] || ">", sign = match[3] || "", symbol = match[4] || "", zfill = match[5], width = +match[6], comma = match[7], precision = match[8], type = match[9], scale = 1, prefix = "", suffix = "", integer = false;
2136+
var match = d3_format_re.exec(specifier), fill = match[1] || " ", align = match[2] || ">", sign = match[3] || "-", symbol = match[4] || "", zfill = match[5], width = +match[6], comma = match[7], precision = match[8], type = match[9], scale = 1, prefix = "", suffix = "", integer = false;
21372137
if (precision) precision = +precision.substring(1);
21382138
if (zfill || fill === "0" && align === "=") {
21392139
zfill = fill = "0";
@@ -2185,7 +2185,7 @@
21852185
return function(value) {
21862186
var fullSuffix = suffix;
21872187
if (integer && value % 1) return "";
2188-
var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign;
2188+
var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign === "-" ? "" : sign;
21892189
if (scale < 0) {
21902190
var unit = d3.formatPrefix(value, precision);
21912191
value = unit.scale(value);

d3.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/locale/number-format.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function d3_locale_numberFormat(locale) {
2525
var match = d3_format_re.exec(specifier),
2626
fill = match[1] || " ",
2727
align = match[2] || ">",
28-
sign = match[3] || "",
28+
sign = match[3] || "-",
2929
symbol = match[4] || "",
3030
zfill = match[5],
3131
width = +match[6],
@@ -80,7 +80,7 @@ function d3_locale_numberFormat(locale) {
8080
if (integer && (value % 1)) return "";
8181

8282
// Convert negative to positive, and record the sign prefix.
83-
var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign;
83+
var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign === "-" ? "" : sign;
8484

8585
// Apply the scale, computing it from the value's exponent for si format.
8686
// Preserve the existing suffix, if any, such as the currency symbol.

test/format/format-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,16 @@ suite.addBatch({
371371
assert.strictEqual(format(" 13,d")(0), " 0");
372372
assert.strictEqual(format(" 21,d")(0), " 0");
373373
},
374+
"explicitly only use a sign for negative numbers": function(format) {
375+
assert.strictEqual(format("-1,d")(-1), "-1");
376+
assert.strictEqual(format("-1,d")(0), "0");
377+
assert.strictEqual(format("-2,d")(0), " 0");
378+
assert.strictEqual(format("-3,d")(0), " 0");
379+
assert.strictEqual(format("-5,d")(0), " 0");
380+
assert.strictEqual(format("-8,d")(0), " 0");
381+
assert.strictEqual(format("-13,d")(0), " 0");
382+
assert.strictEqual(format("-21,d")(0), " 0");
383+
},
374384
"can format negative zero": function(format) {
375385
assert.strictEqual(format("1d")(-0), "-0");
376386
assert.strictEqual(format("1f")(-0), "-0");

0 commit comments

Comments
 (0)