Skip to content

Commit a31f592

Browse files
committed
Fix rounded format specifier; fixes d3#1042.
1 parent 183060d commit a31f592

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

d3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,11 @@
634634
return x.toFixed(p);
635635
},
636636
r: function(x, p) {
637-
return d3.round(x, p = d3_format_precision(x, p)).toFixed(Math.max(0, Math.min(20, p)));
637+
return (x = d3.round(x, d3_format_precision(x, p))).toFixed(Math.max(0, Math.min(20, d3_format_precision(x * (1 + ε), p))));
638638
}
639639
});
640640
function d3_format_precision(x, p) {
641-
return p - (x ? 1 + Math.floor(Math.log(x + Math.pow(10, 1 + Math.floor(Math.log(x) / Math.LN10) - p)) / Math.LN10) : 1);
641+
return p - (x ? Math.ceil(Math.log(x) / Math.LN10) : 1);
642642
}
643643
function d3_format_typeDefault(x) {
644644
return x + "";

d3.min.js

Lines changed: 4 additions & 4 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ var d3_format_types = d3.map({
9595
g: function(x, p) { return x.toPrecision(p); },
9696
e: function(x, p) { return x.toExponential(p); },
9797
f: function(x, p) { return x.toFixed(p); },
98-
r: function(x, p) { return d3.round(x, p = d3_format_precision(x, p)).toFixed(Math.max(0, Math.min(20, p))); }
98+
r: function(x, p) { return (x = d3.round(x, d3_format_precision(x, p))).toFixed(Math.max(0, Math.min(20, d3_format_precision(x * (1 + ε), p)))); }
9999
});
100100

101101
function d3_format_precision(x, p) {
102-
return p - (x ? 1 + Math.floor(Math.log(x + Math.pow(10, 1 + Math.floor(Math.log(x) / Math.LN10) - p)) / Math.LN10) : 1);
102+
return p - (x ? Math.ceil(Math.log(x) / Math.LN10) : 1);
103103
}
104104

105105
function d3_format_typeDefault(x) {

test/core/format-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ suite.addBatch({
126126
"can round to significant digits": function(format) {
127127
assert.strictEqual(format(".2r")(0), "0.0");
128128
assert.strictEqual(format(".1r")(0.049), "0.05");
129+
assert.strictEqual(format(".1r")(-0.049), "-0.05");
129130
assert.strictEqual(format(".1r")(0.49), "0.5");
131+
assert.strictEqual(format(".1r")(-0.49), "-0.5");
130132
assert.strictEqual(format(".2r")(0.449), "0.45");
131133
assert.strictEqual(format(".3r")(0.4449), "0.445");
132134
assert.strictEqual(format(".3r")(1.00), "1.00");
@@ -139,6 +141,10 @@ suite.addBatch({
139141
assert.strictEqual(format(".4r")(123.45), "123.5");
140142
assert.strictEqual(format(".5r")(123.45), "123.45");
141143
assert.strictEqual(format(".6r")(123.45), "123.450");
144+
assert.strictEqual(format(".1r")(.9), "0.9");
145+
assert.strictEqual(format(".1r")(.09), "0.09");
146+
assert.strictEqual(format(".1r")(.949), "0.9");
147+
assert.strictEqual(format(".1r")(.0949), "0.09");
142148
},
143149
"can round very small numbers": function(format) {
144150
var f = format(".2r");

0 commit comments

Comments
 (0)