Permalink
Browse files

Tweak comment. Add another test.

  • Loading branch information...
1 parent 5c3d51c commit 0cb6cd9283bc8601aee42e397d4cce40adb08925 @mbostock mbostock committed Feb 18, 2014
Showing with 29 additions and 9 deletions.
  1. +3 −2 d3.js
  2. +1 −1 d3.min.js
  3. +4 −4 src/locale/number-format.js
  4. +20 −0 test/format/format-test.js
  5. +1 −2 test/locale/locale-test.js
View
@@ -2191,12 +2191,13 @@
type = d3_format_types.get(type) || d3_format_typeDefault;
var zcomma = zfill && comma;
return function(value) {
+ var fullSuffix = suffix;
if (integer && value % 1) return "";
var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign;
if (scale < 0) {
var unit = d3.formatPrefix(value, precision);
value = unit.scale(value);
- suffix = unit.symbol;
+ fullSuffix = unit.symbol + suffix;
} else {
value *= scale;
}
@@ -2207,7 +2208,7 @@
if (zcomma) before = formatGroup(padding + before);
negative += prefix;
value = before + after;
- return (align === "<" ? negative + value + padding : align === ">" ? padding + negative + value : align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length) : negative + (zcomma ? value : padding + value)) + suffix;
+ return (align === "<" ? negative + value + padding : align === ">" ? padding + negative + value : align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length) : negative + (zcomma ? value : padding + value)) + fullSuffix;
};
};
}
View
@@ -74,8 +74,7 @@ function d3_locale_numberFormat(locale) {
var zcomma = zfill && comma;
return function(value) {
- // Local variable to prevent side effect
- var localSuffix = suffix;
+ var fullSuffix = suffix;
// Return the empty string for floats formatted as ints.
if (integer && (value % 1)) return "";
@@ -84,10 +83,11 @@ function d3_locale_numberFormat(locale) {
var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign;
// Apply the scale, computing it from the value's exponent for si format.
+ // Preserve the existing suffix, if any, such as the currency symbol.
if (scale < 0) {
var unit = d3.formatPrefix(value, precision);
value = unit.scale(value);
- localSuffix = unit.symbol + localSuffix;
+ fullSuffix = unit.symbol + suffix;
} else {
value *= scale;
}
@@ -118,7 +118,7 @@ function d3_locale_numberFormat(locale) {
return (align === "<" ? negative + value + padding
: align === ">" ? padding + negative + value
: align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length)
- : negative + (zcomma ? value : padding + value)) + localSuffix;
+ : negative + (zcomma ? value : padding + value)) + fullSuffix;
};
};
}
@@ -101,6 +101,26 @@ suite.addBatch({
assert.strictEqual(f(999500), "999.5k");
@CathrineSilje
CathrineSilje Nov 8, 2014

(zcomma?) ">" ? paddin + negative + value 👍 ' 💯

assert.strictEqual(f(.009995), "9.995m");
},
+ "can output SI prefix notation with appropriate rounding and currency symbol": function(format) {
+ var f = format("$.3s");
+ assert.strictEqual(f(0), "$0.00");
+ assert.strictEqual(f(1), "$1.00");
+ assert.strictEqual(f(10), "$10.0");
+ assert.strictEqual(f(100), "$100");
+ assert.strictEqual(f(999.5), "$1.00k");
+ assert.strictEqual(f(999500), "$1.00M");
+ assert.strictEqual(f(1000), "$1.00k");
+ assert.strictEqual(f(1500.5), "$1.50k");
+ assert.strictEqual(f(145500000), "$146M");
+ assert.strictEqual(f(145999999.999999347), "$146M");
+ assert.strictEqual(f(1e26), "$100Y");
+ assert.strictEqual(f(.000001), "$1.00µ");
+ assert.strictEqual(f(.009995), "$0.0100");
+ var f = format("$.4s");
+ assert.strictEqual(f(999.5), "$999.5");
+ assert.strictEqual(f(999500), "$999.5k");
+ assert.strictEqual(f(.009995), "$9.995m");
+ },
"can output a currency": function(format) {
var f = format("$");
assert.strictEqual(f(0), "$0");
@@ -22,11 +22,10 @@ suite.addBatch({
var f = format("$,.2f");
assert.equal(f(12345.67), "12 345,67 руб.");
},
- "formats currencies": function(format) {
+ "formats currencies with SI-prefix notation and currency suffix": function(format) {
var f = format("$,.4s");
assert.equal(f(12345.67), "12,35k руб.");
}
-
},
"timeFormat": {

0 comments on commit 0cb6cd9

Please sign in to comment.