Skip to content

Commit

Permalink
Some type K abbreviations can be omitted in locale
Browse files Browse the repository at this point in the history
  • Loading branch information
davinov committed Nov 29, 2019
1 parent cfef0ad commit e03ec09
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ Returns a *locale* object for the specified *definition* with [*locale*.format](
* `thousands` - the group separator (e.g., `","`).
* `grouping` - the array of group sizes (e.g., `[3]`), cycled as needed.
* `currency` - the currency prefix and suffix (e.g., `["$", ""]`).
* `currencyAbbreviations` - the list of abbreviated suffixes for currency vales (array of 5 elements: units, thousands, millions, billions and trillions) (default `["", "K", "M", "B", "T"]`).
* `currencyAbbreviations` - the list of abbreviated suffixes for currency values; an array of elements for each: units, thousands, millions, billions and trillions; defaults to `["", "K", "M", "B", "T"]`. The number of elements can vary.
* `numerals` - optional; an array of ten strings to replace the numerals 0-9.
* `percent` - optional; the percent sign (defaults to `"%"`).
* `minus` - optional; the minus sign (defaults to hyphen-minus, `"-"`).
Expand Down
3 changes: 2 additions & 1 deletion locale/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0"],
"percent": "\u202f%"
"percent": "\u202f%",
"currencyAbbreviations": ["", "K", "Mio", "Mrd"]
}
6 changes: 4 additions & 2 deletions src/formatPrefixAuto.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ function formatSignificantDigitsForPrefixes(x, p, minPrefixOrder, maxPrefixOrder
: "0." + new Array(1 - i).join("0") + formatDecimal(x, Math.max(0, p + i - 1))[0]; // less than the smallest prefix
}

export function formatCurrencyPrefixAuto(x, p) {
return formatSignificantDigitsForPrefixes(x, p, 0, 4);
export function createFormatCurrencyPrefixAutoForLocale(currencyAbbreviations) {
return function formatCurrencyPrefixAuto(x, p) {
return formatSignificantDigitsForPrefixes(x, p, 0, currencyAbbreviations.length - 1);
}
}

export default function(x, p) {
Expand Down
4 changes: 2 additions & 2 deletions src/formatTypes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import formatPrefixAuto, { formatCurrencyPrefixAuto } from "./formatPrefixAuto.js";
import formatPrefixAuto, { createFormatCurrencyPrefixAutoForLocale } from "./formatPrefixAuto.js";
import formatRounded from "./formatRounded.js";

export default {
Expand All @@ -9,7 +9,7 @@ export default {
"e": function(x, p) { return x.toExponential(p); },
"f": function(x, p) { return x.toFixed(p); },
"g": function(x, p) { return x.toPrecision(p); },
"K": formatCurrencyPrefixAuto,
"K": createFormatCurrencyPrefixAutoForLocale, // depends of the current locale
"o": function(x) { return Math.round(x).toString(8); },
"p": function(x, p) { return formatRounded(x * 100, p); },
"r": formatRounded,
Expand Down
5 changes: 4 additions & 1 deletion src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export default function(locale) {
var formatType = formatTypes[type],
maybeSuffix = /[defgKprs%]/.test(type);

if (type === 'K')
formatType = formatType(currencyAbbreviations);

// Set the default precision if not specified,
// or clamp the specified precision to the supported range.
// For significant precision, it must be in [1, 21].
Expand Down Expand Up @@ -151,7 +154,7 @@ export default function(locale) {
}

var formatPrefix = createFormatPrefix(SIprefixes, -8, 8);
var formatCurrencyPrefix = createFormatPrefix(currencyAbbreviations, 0, 4);
var formatCurrencyPrefix = createFormatPrefix(currencyAbbreviations, 0, currencyAbbreviations.length - 1);

return {
format: newFormat,
Expand Down
5 changes: 5 additions & 0 deletions test/locale-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ tape("formatLocale({currencyAbbreviations: [list of abbreviations]}) should abbr
test.end();
});

tape("formatLocale({currencyAbbreviations: [list of abbreviations]}) should abbreviate only specified levels", function (test) {
test.equal(d3.formatLocale({ currencyAbbreviations: ["", "M", "Mio", "Mrd"] }).format("$.3K")(1.2e12), "1200Mrd");
test.end();
});

tape("formatLocale({grouping: undefined}) does not perform any grouping", function(test) {
test.equal(d3.formatLocale({decimal: "."}).format("012,.2f")(2), "000000002.00");
test.end();
Expand Down

0 comments on commit e03ec09

Please sign in to comment.