diff --git a/src/locale.js b/src/locale.js index 59639a8..b500b10 100644 --- a/src/locale.js +++ b/src/locale.js @@ -64,6 +64,21 @@ export default function(locale) { // Perform the initial formatting. value = formatType(value, precision); + // If the original value was negative, it may be rounded to zero during + // formatting; treat this as (positive) zero. + if (valueNegative) { + var i = -1, n = value.length, c; + valueNegative = false; + while (++i < n) { + if (c = value.charCodeAt(i), (48 < c && c < 58) + || (type === "x" && 96 < c && c < 103) + || (type === "X" && 64 < c && c < 71)) { + valueNegative = true; + break; + } + } + } + // Compute the prefix and suffix. valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix; valueSuffix = valueSuffix + (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + (valueNegative && sign === "(" ? ")" : ""); diff --git a/test/format-type-d-test.js b/test/format-type-d-test.js index a2460db..ccdfc7a 100644 --- a/test/format-type-d-test.js +++ b/test/format-type-d-test.js @@ -225,7 +225,8 @@ tape("format(\"-,d\") explicitly only use a sign for negative numbers", function test.end(); }); -tape("format(\"d\") can format negative zero", function(test) { - test.equal(format.format("1d")(-0), "-0"); +tape("format(\"d\") can format negative zero as zero", function(test) { + test.equal(format.format("1d")(-0), "0"); + test.equal(format.format("1d")(-1e-12), "0"); test.end(); }); diff --git a/test/format-type-e-test.js b/test/format-type-e-test.js index c59939c..ade3916 100644 --- a/test/format-type-e-test.js +++ b/test/format-type-e-test.js @@ -16,6 +16,12 @@ tape("format(\"e\") can output exponent notation", function(test) { test.end(); }); +tape("format(\"e\") can format negative zero as zero", function(test) { + test.equal(format.format("1e")(-0), "0.000000e+0"); + test.equal(format.format("1e")(-1e-12), "-1.000000e-12"); + test.end(); +}); + tape("format(\",e\") does not group Infinity", function(test) { test.equal(format.format(",e")(Infinity), "Infinity"); test.end(); diff --git a/test/format-type-f-test.js b/test/format-type-f-test.js index 82f8904..1a301f1 100644 --- a/test/format-type-f-test.js +++ b/test/format-type-f-test.js @@ -40,8 +40,9 @@ tape("format(\"f\") can display integers in fixed-point notation", function(test test.end(); }); -tape("format(\"f\") can format negative zero", function(test) { - test.equal(format.format("1f")(-0), "-0.000000"); +tape("format(\"f\") can format negative zero as zero", function(test) { + test.equal(format.format("1f")(-0), "0.000000"); + test.equal(format.format("1f")(-1e-12), "0.000000"); test.end(); }); diff --git a/test/format-type-x-test.js b/test/format-type-x-test.js index bd00b1f..1b6c5d0 100644 --- a/test/format-type-x-test.js +++ b/test/format-type-x-test.js @@ -50,6 +50,11 @@ tape("format(\"x\") rounds non-integers", function(test) { test.end(); }); +tape("format(\"x\") does not consider -0xeee to be positive", function(test) { + test.equal(format.format("x")(-0xeee), "-eee"); + test.end(); +}); + tape("format(\"X\") returns the expected hexadecimal (uppercase) string", function(test) { test.equal(format.format("X")(0xdeadbeef), "DEADBEEF"); test.end(); @@ -60,6 +65,11 @@ tape("format(\"#X\") returns the expected hexadecimal (uppercase) string with pr test.end(); }); +tape("format(\"X\") does not consider -0xeee to be positive", function(test) { + test.equal(format.format("X")(-0xeee), "-EEE"); + test.end(); +}); + tape("format(\"#[width]x\") considers the prefix", function(test) { test.equal(format.format("20x")(0xdeadbeef), " deadbeef"); test.equal(format.format("#20x")(0xdeadbeef), " 0xdeadbeef");