Skip to content

Commit

Permalink
Fix rounding error with values less than 1y.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 15, 2015
1 parent c83ad05 commit dd9c08e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 69 deletions.
21 changes: 0 additions & 21 deletions src/formatAutoPrefix.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/formatPrefixAuto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import formatDecimal from "./formatDecimal";

export var prefixExponent;

export default function(x, p) {
var d = formatDecimal(x, p);
if (!d) return x + "";
var coefficient = d[0],
exponent = d[1],
i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,
n = coefficient.length;
return i === n ? coefficient
: i > n ? coefficient + new Array(i - n + 1).join("0")
: i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i)
: "0." + new Array(1 - i).join("0") + formatDecimal(x, p + i - 1)[0]; // less than 1y!
};
12 changes: 4 additions & 8 deletions src/formatRounded.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ export default function(x, p) {
var d = formatDecimal(x, p);
if (!d) return x + "";
var coefficient = d[0],
exponent = d[1],
i = exponent + 1;
return i <= 0
? "0." + new Array(1 - i).join("0") + coefficient
: coefficient.slice(0, i)
+ (coefficient.length > i
? "." + coefficient.slice(i)
: new Array(i - coefficient.length + 1).join("0"));
exponent = d[1];
return exponent < 0 ? "0." + new Array(-exponent).join("0") + coefficient
: coefficient.length > exponent + 1 ? coefficient.slice(0, exponent + 1) + "." + coefficient.slice(exponent + 1)
: coefficient + new Array(exponent - coefficient.length + 2).join("0");
};
15 changes: 0 additions & 15 deletions src/formatRoundedPercentage.js

This file was deleted.

7 changes: 3 additions & 4 deletions src/formatTypes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import formatAutoPrefix from "./formatAutoPrefix";
import formatDefault from "./formatDefault";
import formatPrefixAuto from "./formatPrefixAuto";
import formatRounded from "./formatRounded";
import formatRoundedPercentage from "./formatRoundedPercentage";

export default {
"": formatDefault,
Expand All @@ -13,9 +12,9 @@ export default {
"f": function(x, p) { return x.toFixed(p); },
"g": function(x, p) { return x.toPrecision(p); },
"o": function(x) { return x.toString(8); },
"p": formatRoundedPercentage,
"p": function(x, p) { return formatRounded(x * 100, p); },
"r": formatRounded,
"s": formatAutoPrefix,
"s": formatPrefixAuto,
"X": function(x) { return x.toString(16).toUpperCase(); },
"x": function(x) { return x.toString(16); }
};
2 changes: 1 addition & 1 deletion src/localeFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import exponent from "./exponent";
import formatGroup from "./formatGroup";
import formatSpecifier from "./formatSpecifier";
import formatTypes from "./formatTypes";
import {prefixExponent} from "./formatAutoPrefix";
import {prefixExponent} from "./formatPrefixAuto";

var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];

Expand Down
40 changes: 20 additions & 20 deletions test/format-type-s-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ tape("format(\"[.precision]s\") outputs SI-prefix notation with precision signif

tape("format(\"s\") formats numbers smaller than 1e-24 with yocto", function(test) {
var f = format.format(".8s");
test.equal(f(1.23e-30), "0.0000012y");
test.equal(f(1.23e-29), "0.0000123y");
test.equal(f(1.23e-28), "0.0001230y");
test.equal(f(1.23e-27), "0.0012300y");
test.equal(f(1.23e-26), "0.0123000y");
test.equal(f(1.23e-25), "0.1230000y");
test.equal(f(1.23e-24), "1.2300000y");
test.equal(f(1.23e-23), "12.300000y");
test.equal(f(1.23e-22), "123.00000y");
test.equal(f(1.23e-21), "1.2300000z");
test.equal(f(-1.23e-30), "-0.0000012y");
test.equal(f(-1.23e-29), "-0.0000123y");
test.equal(f(-1.23e-28), "-0.0001230y");
test.equal(f(-1.23e-27), "-0.0012300y");
test.equal(f(-1.23e-26), "-0.0123000y");
test.equal(f(-1.23e-25), "-0.1230000y");
test.equal(f(-1.23e-24), "-1.2300000y");
test.equal(f(-1.23e-23), "-12.300000y");
test.equal(f(-1.23e-22), "-123.00000y");
test.equal(f(-1.23e-21), "-1.2300000z");
test.equal(f(1.29e-30), "0.0000013y"); // Note: rounded!
test.equal(f(1.29e-29), "0.0000129y");
test.equal(f(1.29e-28), "0.0001290y");
test.equal(f(1.29e-27), "0.0012900y");
test.equal(f(1.29e-26), "0.0129000y");
test.equal(f(1.29e-25), "0.1290000y");
test.equal(f(1.29e-24), "1.2900000y");
test.equal(f(1.29e-23), "12.900000y");
test.equal(f(1.29e-22), "129.00000y");
test.equal(f(1.29e-21), "1.2900000z");
test.equal(f(-1.29e-30), "-0.0000013y"); // Note: rounded!
test.equal(f(-1.29e-29), "-0.0000129y");
test.equal(f(-1.29e-28), "-0.0001290y");
test.equal(f(-1.29e-27), "-0.0012900y");
test.equal(f(-1.29e-26), "-0.0129000y");
test.equal(f(-1.29e-25), "-0.1290000y");
test.equal(f(-1.29e-24), "-1.2900000y");
test.equal(f(-1.29e-23), "-12.900000y");
test.equal(f(-1.29e-22), "-129.00000y");
test.equal(f(-1.29e-21), "-1.2900000z");
test.end();
});

Expand Down

0 comments on commit dd9c08e

Please sign in to comment.