Skip to content

Commit

Permalink
Fix for formatDecimal when p is undefined.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 15, 2015
1 parent fe9be36 commit 77b546d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/formatDecimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// significant digits p, where x is positive and p is in [1, 21] or undefined.
// For example, formatDecimal(1.23) returns ["123", -1].
export default function(x, p) {
if ((i = (x = x.toExponential(p - 1)).indexOf("e")) < 0) return null; // NaN, ±Infinity
if ((i = (x = x.toExponential(p && p - 1)).indexOf("e")) < 0) return null; // NaN, ±Infinity
var i, coefficient = x.slice(0, i);

// The string returned by toExponential either has the form \d\.\d+e[-+]\d+
Expand Down
12 changes: 12 additions & 0 deletions test/precisionFixed-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var tape = require("tape"),
format = require("../");

tape("precisionFixed(number) returns the expected value", function(test) {
test.equal(format.precisionFixed(8.9), 0);
test.equal(format.precisionFixed(1.1), 0);
test.equal(format.precisionFixed(0.89), 1);
test.equal(format.precisionFixed(0.11), 1);
test.equal(format.precisionFixed(0.089), 2);
test.equal(format.precisionFixed(0.011), 2);
test.end();
});

0 comments on commit 77b546d

Please sign in to comment.