-
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
Currency abbreviations #81
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e249b2e
Add formatCurrencyPrefix and currencyPrecisionPrefix
davinov d1ed244
Introduce K type for currency prefix
davinov fad5de7
Documentation for type K and associated methods
davinov 3fa19c7
Allow currency abbreviations in locale definition
davinov 7ee5a2c
dd currency abbreviations for en-GB
davinov a0de748
Some type K abbreviations can be omitted in locale
davinov 76c6681
Use CLDR for currencyAbbreviations
davinov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createPrecisionPrefix } from "./precisionPrefix.js"; | ||
|
||
export default createPrecisionPrefix(0, 4); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import exponent from "./exponent.js"; | ||
|
||
export default function(step, value) { | ||
return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step))); | ||
export function createPrecisionPrefix(minimumPrefixOrder, maximumPrefixOrder) { | ||
return function (step, value) { | ||
return Math.max(0, Math.max(minimumPrefixOrder, Math.min(maximumPrefixOrder, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step))); | ||
} | ||
} | ||
|
||
export default createPrecisionPrefix(-8, 8); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
var tape = require("tape"), | ||
format = require("../"); | ||
|
||
// For currencies, only 4 prefixes are commonly used: | ||
// thousands (K), millions (M), billions (B) and trillions (T) | ||
|
||
tape("precisionPrefix(step, value) returns zero between 1 and 100 (unit step)", function (test) { | ||
test.equal(format.currencyPrecisionPrefix(1e+0, 1e+0), 0); // 1 | ||
test.equal(format.currencyPrecisionPrefix(1e+0, 1e+1), 0); // 10 | ||
test.equal(format.currencyPrecisionPrefix(1e+0, 1e+2), 0); // 100 | ||
test.end() | ||
}); | ||
|
||
tape("precisionPrefix(step, value) returns zero between 1 and 100 (thousand step)", function (test) { | ||
test.equal(format.currencyPrecisionPrefix(1e+3, 1e+3), 0); // 1K | ||
test.equal(format.currencyPrecisionPrefix(1e+3, 1e+4), 0); // 10K | ||
test.equal(format.currencyPrecisionPrefix(1e+3, 1e+5), 0); // 100K | ||
test.end() | ||
}); | ||
|
||
tape("precisionPrefix(step, value) returns zero between 1 and 100 (million step)", function (test) { | ||
test.equal(format.currencyPrecisionPrefix(1e+6, 1e+6), 0); // 1M | ||
test.equal(format.currencyPrecisionPrefix(1e+6, 1e+7), 0); // 10M | ||
test.equal(format.currencyPrecisionPrefix(1e+6, 1e+8), 0); // 100M | ||
test.end() | ||
}); | ||
|
||
tape("precisionPrefix(step, value) returns zero between 1 and 100 (billion step)", function (test) { | ||
test.equal(format.currencyPrecisionPrefix(1e+9, 1e+9), 0); // 1B | ||
test.equal(format.currencyPrecisionPrefix(1e+9, 1e+10), 0); // 10B | ||
test.equal(format.currencyPrecisionPrefix(1e+9, 1e+11), 0); // 100B | ||
test.end() | ||
}); | ||
|
||
tape("currencyPrecisionPrefix(step, value) returns the expected precision when value is greater than one trillion", function(test) { | ||
test.equal(format.currencyPrecisionPrefix(1e+12, 1e+12), 0); // 1T | ||
test.equal(format.currencyPrecisionPrefix(1e+12, 1e+13), 0); // 10T | ||
test.equal(format.currencyPrecisionPrefix(1e+12, 1e+14), 0); // 100T | ||
test.equal(format.currencyPrecisionPrefix(1e+12, 1e+15), 0); // 1000T | ||
test.equal(format.currencyPrecisionPrefix(1e+11, 1e+15), 1); // 1000.0T | ||
test.end(); | ||
}); | ||
|
||
tape("currencyPrecisionPrefix(step, value) returns the expected precision when value is less than one unit", function(test) { | ||
test.equal(format.currencyPrecisionPrefix(1e+0, 1e+0), 0); // 1 | ||
test.equal(format.currencyPrecisionPrefix(1e-1, 1e-1), 1); // 0.1 | ||
test.equal(format.currencyPrecisionPrefix(1e-2, 1e-2), 2); // 0.01 | ||
test.equal(format.currencyPrecisionPrefix(1e-3, 1e-3), 3); // 0.001 | ||
test.equal(format.currencyPrecisionPrefix(1e-4, 1e-4), 4); // 0.0001 | ||
test.end(); | ||
}); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
From Language Matters: Millions, Billions and Other Large Numbers
Might we consider changing
locale/en-GB.json
to match these?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.
changed in #96
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.
Thanks for adding these. I removed them from this PR originally (7ee5a2c) because they are not indicated in the CDLR, but I think they should be!