Permalink
Browse files

Fix another rounding bug with SI-format.

Turns out 1400 * .001 is 1.4000000000000001, but 1400 / 1000 is 1.4.
  • Loading branch information...
1 parent c384fad commit 00bc3f05a72a9f9f7cc262f0903b16ecb2fd6f88 @mbostock mbostock committed Apr 26, 2012
Showing with 11 additions and 10 deletions.
  1. +3 −3 d3.v2.js
  2. +4 −4 d3.v2.min.js
  3. +1 −1 src/core/format.js
  4. +2 −2 src/core/formatPrefix.js
  5. +1 −0 test/core/format-test.js
View
6 d3.v2.js
@@ -661,7 +661,7 @@ d3.format = function(specifier) {
// Apply the scale, computing it from the value's exponent for si format.
if (scale < 0) {
var prefix = d3.formatPrefix(value, precision);
- value *= prefix.scale;
+ value = prefix.scale(value);
suffix = prefix.symbol;
} else {
value *= scale;
@@ -730,12 +730,12 @@ d3.formatPrefix = function(value, precision) {
};
function d3_formatPrefix(d, i) {
+ var k = Math.pow(10, Math.abs(8 - i) * 3)
return {
- scale: Math.pow(10, (8 - i) * 3),
+ scale: i > 8 ? function(d) { return d / k; } : function(d) { return d * k; },
symbol: d
};
}
-
/*
* TERMS OF USE - EASING EQUATIONS
*
View
8 d3.v2.min.js
4 additions, 4 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
2 src/core/format.js
@@ -43,7 +43,7 @@ d3.format = function(specifier) {
// Apply the scale, computing it from the value's exponent for si format.
if (scale < 0) {
var prefix = d3.formatPrefix(value, precision);
- value *= prefix.scale;
+ value = prefix.scale(value);
suffix = prefix.symbol;
} else {
value *= scale;
View
4 src/core/formatPrefix.js
@@ -12,9 +12,9 @@ d3.formatPrefix = function(value, precision) {
};
function d3_formatPrefix(d, i) {
+ var k = Math.pow(10, Math.abs(8 - i) * 3);
return {
- scale: Math.pow(10, (8 - i) * 3),
+ scale: i > 8 ? function(d) { return d / k; } : function(d) { return d * k; },
symbol: d
};
}
-
View
1 test/core/format-test.js
@@ -78,6 +78,7 @@ suite.addBatch({
assert.strictEqual(f(999.5), "999.5");
assert.strictEqual(f(999500), "999.5k");
assert.strictEqual(f(1000), "1k");
+ assert.strictEqual(f(1400), "1.4k");
assert.strictEqual(f(1500.5), "1.5005k");
assert.strictEqual(f(.000001), "");
},

0 comments on commit 00bc3f0

Please sign in to comment.