Skip to content

Commit

Permalink
Fix d3#2595 - type coercion in formatPrefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Oct 23, 2015
1 parent 6b15dd1 commit 07a8737
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -2187,7 +2187,7 @@
var d3_formatPrefixes = [ "y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y" ].map(d3_formatPrefix);
d3.formatPrefix = function(value, precision) {
var i = 0;
if (value) {
if (value = +value) {
if (value < 0) value *= -1;
if (precision) value = d3.round(value, d3_format_precision(value, precision));
i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10);
Expand Down
2 changes: 1 addition & 1 deletion d3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/format/formatPrefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var d3_formatPrefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P"

d3.formatPrefix = function(value, precision) {
var i = 0;
if (value) {
if (value = +value) {
if (value < 0) value *= -1;
if (precision) value = d3.round(value, d3_format_precision(value, precision));
i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10);
Expand Down
3 changes: 3 additions & 0 deletions test/format/formatPrefix-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ suite.addBatch({
assert.equal(prefix(0.0009950, 2).symbol, "m");
assert.equal(prefix(0.0009500, 2).symbol, "µ");
assert.equal(prefix(0.0009500, 1).symbol, "m");
},
"coerces input to a string": function(prefix) {
assert.equal(prefix("0").symbol, "");
}
}
});
Expand Down

0 comments on commit 07a8737

Please sign in to comment.