Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Binary prefix, Currency prefix (Merge #70 #81) #96

Closed
wants to merge 18 commits into from

Conversation

Fil
Copy link
Member

@Fil Fil commented Jul 7, 2020

This branch merges #70 and #81 which were targeting the same set of lines.

@Fil Fil mentioned this pull request Jul 7, 2020
@Fil

This comment has been minimized.

@Fil Fil changed the title Merge Merge #70 #81 Jul 7, 2020
@Fil Fil mentioned this pull request Jul 7, 2020
Copy link

@curran curran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is looking fantastic! Commented about a few minor things here.

README.md Outdated Show resolved Hide resolved
<a name="locale_formatCurrencyPrefix" href="#locale_formatCurrencyPrefix">#</a> <i>locale</i>.<b>formatCurrencyPrefix</b>(<i>specifier</i>, <i>value</i>) [<>](https://github.com/d3/d3-format/blob/master/src/locale.js#L153 "Source")

Equivalent to [*locale*.locale_formatPrefix](#locale_formatPrefix), except it uses common currency abbreviations:

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to mention here that these are for US English locale specifically.

@@ -290,6 +301,10 @@ f(1.2e6); // "1.2M"
f(1.3e6); // "1.3M"
```

<a name="currencyPrecisionPrefix" href="#currencyPrecisionPrefix">#</a> d3.<b>currencyPrecisionPrefix</b>(<i>step</i>, <i>value</i>) [<>](https://github.com/d3/d3-format/blob/master/src/currencyPrecisionPrefix.js "Source")
Copy link

@curran curran Jul 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these necessarily related to currency? IMO this formatter is useful for formatting large numbers in general, not specific to dealing with currency. Although, the currency use case gives some concrete grounding for decisions, which is great. Probably it's fine to call it currencyPrecisionPrefix, but it's actually a more general formatter.

src/index.js Outdated Show resolved Hide resolved
Fil and others added 2 commits July 8, 2020 18:06
Co-authored-by: Curran Kelleher <[email protected]>
Co-authored-by: Curran Kelleher <[email protected]>
Copy link

@curran curran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great.

@Fil Fil added the feature label Jul 10, 2020
@Fil Fil changed the title Merge #70 #81 Implement Binary prefix, Currency prefix (Merge #70 #81) Jul 27, 2020
@Fil Fil requested a review from mbostock July 27, 2020 08:24
@mbostock
Copy link
Member

The currency “prefix” feature is misnamed: this is not a prefix, it’s a symbol, or even a suffix based on how it’s formatted. In the case of SI, it’s called a prefix because it’s used at the start of a word: for example, “k” stands for kilo as in “kilogram” or “kilobyte”. Since we don’t talk about “kilodollars” and we’re using the “k” symbol at the end, I don’t think it’s appropriate to call it a prefix. (In retrospect, d3.formatPrefix should probably have been named to d3.formatSi, but that ship has sailed.)

More generally, I’m wary of adding new localization features to this library. The binary prefixes seem reasonable because they correspond closely to the existing SI prefixes and are well-defined, but the currency formatting feature feels… a little ad hoc and I question how useful it is for all locales. In the long run, I’d prefer to use the standard JavaScript APIs for number and date localization and I’m generally opposed to expanding the surface area of this library.

@Fil Fil mentioned this pull request Aug 13, 2020
Closed
2 tasks
@runarberg
Copy link

Should we go ahead with #70 since there is no consensus on adding currency formatting?

@nimisha1921
Copy link

@Fil @curran Any plan on this? As there are open source data visualization tools that depend on d3format and if this "human consumption filesize" introduce in d3format, then it will be very helpful to the community.

@runarberg
Copy link

runarberg commented Jun 22, 2021

@nimisha1921 I’ve just been rolling my own using Math.log2 and Intl.NumberFormat:

const PREFIXES = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];

function binaryFormatParts(num: number) {
  if (!Number.isFinite(num) || Math.abs(num) < 1) {
    return [num, 0];
  }

  const exponent = Math.log2(Math.abs(num));
  const prefix = Math.min(Math.trunc(exponent / 10), PREFIXES.length - 1);

  return [num / 2 ** (prefix * 10), prefix];
}

export function binaryFormat(num: number): string {
  const [x, prefix] = binaryFormatParts(num);
  const displayNumber = new Intl.NumberFormat('default', {
    maximumFractionDigits: 2,
    style: 'decimal',
  }).format(x);

  return `${displayNumber} ${PREFIXES[prefix]}B`;
}

@Ayesha-Siddiqa2
Copy link

@Fil @mbostock @curran Any plan or timeline for merging this PR??

@curran
Copy link

curran commented Oct 26, 2021

I have no say as to whether it gets merged or not. Looks good to me at a high level, but I have not considered all the details and rammifications it may have for the future.

@Fil
Copy link
Member Author

Fil commented Aug 23, 2023

Closing, following @runarberg’s comment #70 (comment) (one should use Intl.NumberFormat instead).

@Fil Fil closed this Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

7 participants