Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
runarberg authored Jun 4, 2020
2 parents 2fb8dbd + e60aab4 commit e33a8f1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d3-format",
"version": "1.4.3",
"version": "1.4.4",
"description": "Format numbers for human consumption.",
"keywords": [
"d3",
Expand Down
8 changes: 5 additions & 3 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ export default function(locale) {
} else {
value = +value;

// Determine the sign. -0 is not less than 0, but 1 / -0 is!
var valueNegative = value < 0 || 1 / value < 0;

// Perform the initial formatting.
var valueNegative = value < 0;
value = isNaN(value) ? nan : formatType(Math.abs(value), precision);

// Trim insignificant zeros.
if (trim) value = formatTrim(value);

// If a negative value rounds to zero during formatting, treat as positive.
if (valueNegative && +value === 0) valueNegative = false;
// If a negative value rounds to zero after formatting, and no explicit positive sign is requested, hide the sign.
if (valueNegative && +value === 0 && sign !== "+") valueNegative = false;

// Compute the prefix and suffix.
valuePrefix = (valueNegative ? (sign === "(" ? sign : minus) : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
Expand Down
5 changes: 5 additions & 0 deletions test/format-type-e-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ tape("format(\",e\") does not group Infinity", function(test) {
test.equal(format.format(",e")(Infinity), "Infinity");
test.end();
});

tape("format(\".3e\") can format negative infinity", function(test) {
test.equal(format.format(".3e")(-Infinity), "-Infinity");
test.end();
});
8 changes: 8 additions & 0 deletions test/format-type-f-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ tape("format(\"f\") can format negative zero as zero", function(test) {
test.end();
});

tape("format(\"+f\") signs negative zero correctly", function(test) {
test.equal(format.format("+f")(-0), "-0.000000");
test.equal(format.format("+f")(+0), "+0.000000");
test.equal(format.format("+f")(-1e-12), "-0.000000");
test.equal(format.format("+f")(+1e-12), "+0.000000");
test.end();
});

tape("format(\"f\") can format negative infinity", function(test) {
test.equal(format.format("f")(-Infinity), "-Infinity");
test.end();
Expand Down

0 comments on commit e33a8f1

Please sign in to comment.