Skip to content

Commit 4946a8c

Browse files
committed
d3.format: support negative zeroes.
1 parent 22bf996 commit 4946a8c

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

d3.v2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@
566566
var zcomma = zfill && comma;
567567
return function(value) {
568568
if (integer && value % 1) return "";
569-
var negative = value < 0 && (value = -value) ? "-" : sign;
569+
var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign;
570570
if (scale < 0) {
571571
var prefix = d3.formatPrefix(value, precision);
572572
value = prefix.scale(value);

d3.v2.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/format.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ d3.format = function(specifier) {
4949
if (integer && (value % 1)) return "";
5050

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

5454
// Apply the scale, computing it from the value's exponent for si format.
5555
if (scale < 0) {

test/core/format-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,20 @@ suite.addBatch({
291291
assert.strictEqual(format("=+13,d")(0), "+ 0");
292292
assert.strictEqual(format("=+21,d")(0), "+ 0");
293293
},
294+
"a space can denote positive numbers": function(format) {
295+
assert.strictEqual(format(" 1,d")(-1), "-1");
296+
assert.strictEqual(format(" 1,d")(0), " 0");
297+
assert.strictEqual(format(" 2,d")(0), " 0");
298+
assert.strictEqual(format(" 3,d")(0), " 0");
299+
assert.strictEqual(format(" 5,d")(0), " 0");
300+
assert.strictEqual(format(" 8,d")(0), " 0");
301+
assert.strictEqual(format(" 13,d")(0), " 0");
302+
assert.strictEqual(format(" 21,d")(0), " 0");
303+
},
304+
"can format negative zero": function(format) {
305+
assert.strictEqual(format("1d")(-0), "-0");
306+
assert.strictEqual(format("1f")(-0), "-0");
307+
},
294308
"supports \"n\" as an alias for \",g\"": function(format) {
295309
var f = format("n");
296310
assert.strictEqual(f(.0042), "0.0042");

0 commit comments

Comments
 (0)