-
Notifications
You must be signed in to change notification settings - Fork 103
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
Conversation
Closes: #33
Co-authored-by: Philippe Rivière <[email protected]>
Co-authored-by: Philippe Rivière <[email protected]>
Co-authored-by: Philippe Rivière <[email protected]>
These functons will be used todetermine which prefixes will be used when formatting currencies. Common prefixes are for thousands (K), millions (M), billions (B) and trillions (T).
Implement en, fr, de, es, it and ln locales See https://github.com/PrestaShop/PrestaShop/blob/develop/localization/CLDR/core/common/main/<locale>.xml for reference
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this 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.
<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: | ||
|
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
Co-authored-by: Curran Kelleher <[email protected]>
Co-authored-by: Curran Kelleher <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great.
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. |
Should we go ahead with #70 since there is no consensus on adding currency formatting? |
@nimisha1921 I’ve just been rolling my own using 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`;
} |
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. |
Closing, following @runarberg’s comment #70 (comment) (one should use Intl.NumberFormat instead). |
This branch merges #70 and #81 which were targeting the same set of lines.