Skip to content

Commit 0cb6cd9

Browse files
committed
Tweak comment. Add another test.
1 parent 5c3d51c commit 0cb6cd9

5 files changed

Lines changed: 29 additions & 9 deletions

File tree

d3.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,12 +2191,13 @@
21912191
type = d3_format_types.get(type) || d3_format_typeDefault;
21922192
var zcomma = zfill && comma;
21932193
return function(value) {
2194+
var fullSuffix = suffix;
21942195
if (integer && value % 1) return "";
21952196
var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign;
21962197
if (scale < 0) {
21972198
var unit = d3.formatPrefix(value, precision);
21982199
value = unit.scale(value);
2199-
suffix = unit.symbol;
2200+
fullSuffix = unit.symbol + suffix;
22002201
} else {
22012202
value *= scale;
22022203
}
@@ -2207,7 +2208,7 @@
22072208
if (zcomma) before = formatGroup(padding + before);
22082209
negative += prefix;
22092210
value = before + after;
2210-
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;
2211+
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;
22112212
};
22122213
};
22132214
}

d3.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/locale/number-format.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ function d3_locale_numberFormat(locale) {
7474
var zcomma = zfill && comma;
7575

7676
return function(value) {
77-
// Local variable to prevent side effect
78-
var localSuffix = suffix;
77+
var fullSuffix = suffix;
7978

8079
// Return the empty string for floats formatted as ints.
8180
if (integer && (value % 1)) return "";
@@ -84,10 +83,11 @@ function d3_locale_numberFormat(locale) {
8483
var negative = value < 0 || value === 0 && 1 / value < 0 ? (value = -value, "-") : sign;
8584

8685
// Apply the scale, computing it from the value's exponent for si format.
86+
// Preserve the existing suffix, if any, such as the currency symbol.
8787
if (scale < 0) {
8888
var unit = d3.formatPrefix(value, precision);
8989
value = unit.scale(value);
90-
localSuffix = unit.symbol + localSuffix;
90+
fullSuffix = unit.symbol + suffix;
9191
} else {
9292
value *= scale;
9393
}
@@ -118,7 +118,7 @@ function d3_locale_numberFormat(locale) {
118118
return (align === "<" ? negative + value + padding
119119
: align === ">" ? padding + negative + value
120120
: align === "^" ? padding.substring(0, length >>= 1) + negative + value + padding.substring(length)
121-
: negative + (zcomma ? value : padding + value)) + localSuffix;
121+
: negative + (zcomma ? value : padding + value)) + fullSuffix;
122122
};
123123
};
124124
}

test/format/format-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ suite.addBatch({
101101
assert.strictEqual(f(999500), "999.5k");
102102
assert.strictEqual(f(.009995), "9.995m");
103103
},
104+
"can output SI prefix notation with appropriate rounding and currency symbol": function(format) {
105+
var f = format("$.3s");
106+
assert.strictEqual(f(0), "$0.00");
107+
assert.strictEqual(f(1), "$1.00");
108+
assert.strictEqual(f(10), "$10.0");
109+
assert.strictEqual(f(100), "$100");
110+
assert.strictEqual(f(999.5), "$1.00k");
111+
assert.strictEqual(f(999500), "$1.00M");
112+
assert.strictEqual(f(1000), "$1.00k");
113+
assert.strictEqual(f(1500.5), "$1.50k");
114+
assert.strictEqual(f(145500000), "$146M");
115+
assert.strictEqual(f(145999999.999999347), "$146M");
116+
assert.strictEqual(f(1e26), "$100Y");
117+
assert.strictEqual(f(.000001), "$1.00µ");
118+
assert.strictEqual(f(.009995), "$0.0100");
119+
var f = format("$.4s");
120+
assert.strictEqual(f(999.5), "$999.5");
121+
assert.strictEqual(f(999500), "$999.5k");
122+
assert.strictEqual(f(.009995), "$9.995m");
123+
},
104124
"can output a currency": function(format) {
105125
var f = format("$");
106126
assert.strictEqual(f(0), "$0");

test/locale/locale-test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ suite.addBatch({
2222
var f = format("$,.2f");
2323
assert.equal(f(12345.67), "12 345,67 руб.");
2424
},
25-
"formats currencies": function(format) {
25+
"formats currencies with SI-prefix notation and currency suffix": function(format) {
2626
var f = format("$,.4s");
2727
assert.equal(f(12345.67), "12,35k руб.");
2828
}
29-
3029
},
3130

3231
"timeFormat": {

0 commit comments

Comments
 (0)