-
-
Notifications
You must be signed in to change notification settings - Fork 36
Add :percent #1094
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
Add :percent #1094
Changes from 5 commits
c50f5d9
c64d781
2cfa500
277649d
2fc35ac
a1dccbf
54ed7b7
90f1b87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -413,6 +413,123 @@ contains an implementation-defined currency value | |
| of the _operand_ of the annotated _expression_, | ||
| together with the resolved options' values. | ||
|
|
||
| #### The `:percent` function | ||
|
|
||
| > [!IMPORTANT] | ||
| > The _function_ `:percent` has a status of **Draft**. | ||
| > It is proposed for inclusion in a future release of this specification and is not Stable. | ||
|
|
||
| The function `:percent` is a selector and formatter for percent values. | ||
|
|
||
| ##### Operands | ||
|
|
||
| The function `:percent` requires a [Number Operand](#number-operands) as its _operand_. | ||
|
|
||
| When either selecting or formatting the _expression_, | ||
| the numeric value of the _operand_ is multiplied by 100. | ||
|
|
||
| ##### Options | ||
|
|
||
| Some options do not have default values defined in this specification. | ||
| The defaults for these options are implementation-dependent. | ||
| In general, the default values for such options depend on the locale, | ||
| the value of other options, or both. | ||
|
|
||
| > [!NOTE] | ||
| > The names of _options_ and their _option values_ were derived from the | ||
| > [options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options) | ||
| > in JavaScript's `Intl.NumberFormat`. | ||
|
|
||
| The following _options_ are REQUIRED to be available on the function `:percent`: | ||
|
|
||
| - `signDisplay` | ||
| - `auto` (default) | ||
| - `always` | ||
| - `exceptZero` | ||
| - `negative` | ||
| - `never` | ||
| - `useGrouping` | ||
| - `auto` (default) | ||
| - `always` | ||
| - `never` | ||
| - `min2` | ||
| - `minimumFractionDigits` | ||
| - _digit size option_, default: `0` | ||
| - `maximumFractionDigits` | ||
| - _digit size option_, default: `0` | ||
| - `minimumSignificantDigits` | ||
| - _digit size option_ | ||
| - `maximumSignificantDigits` | ||
| - _digit size option_ | ||
| - `trailingZeroDisplay` | ||
| - `auto` (default) | ||
| - `stripIfInteger` | ||
| - `roundingPriority` | ||
| - `auto` (default) | ||
| - `morePrecision` | ||
| - `lessPrecision` | ||
| - `roundingMode` | ||
| - `ceil` | ||
| - `floor` | ||
| - `expand` | ||
| - `trunc` | ||
| - `halfCeil` | ||
| - `halfFloor` | ||
| - `halfExpand` (default) | ||
| - `halfTrunc` | ||
| - `halfEven` | ||
|
|
||
| The numeric value of the _operand_ is multiplied by 100 | ||
| at the start of formatting or selection. | ||
| Each _option_ is applied to the formatted (or selected) value | ||
| rather than the unaltered value of the _operand_. | ||
|
|
||
| > For example, this _placeholder_: | ||
| > | ||
| > ``` | ||
| > {0.1234 :percent maximumFractionDigits=1} | ||
| > ``` | ||
| > | ||
| > might be formatted as "12.3%" in an English locale. | ||
|
|
||
| If the _operand_ of the _expression_ is an implementation-defined type, | ||
| such as the _resolved value_ of an _expression_ with a `:number` or `:integer` _annotation_, | ||
| it can include option values. | ||
| In general, these are included in the resolved option values of the _expression_, | ||
| with _options_ on the _expression_ taking priority over any options of the _operand_. | ||
| Options with the following names are however discarded if included in the _operand_: | ||
|
|
||
| - `minimumIntegerDigits` | ||
| - `roundingIncrement` | ||
| - `select` | ||
|
|
||
| ##### Resolved Value | ||
|
|
||
| The _resolved value_ of an _expression_ with a `:percent` _function_ | ||
aphillips marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| contains an implementation-defined numerical value | ||
| of the _operand_ of the annotated _expression_ | ||
| together with the resolved options' values. | ||
| The _resolved value_ is not altered by `:percent`, | ||
| that is, its numerical value is not multiplied by 100. | ||
|
||
|
|
||
| ##### Selection | ||
|
|
||
| The _function_ `:percent` performs selection as described in [Number Selection](#number-selection) below. | ||
| This selection always uses the `plural` selection mode, | ||
| and is performed on the numerical value of the _operand_ | ||
| multiplied by 100. | ||
|
|
||
| > For example, this _message_: | ||
| > ``` | ||
| > .local $pct = {1 :percent} | ||
| > .match $pct | ||
| > 1 {{Would match with 0.01 as the operand}} | ||
| > 100 {{Matches 💯}} | ||
| > * {{Otherwise}} | ||
| > ``` | ||
| > | ||
| > would be formatted as "Matches 💯". | ||
|
|
||
| #### The `:unit` function | ||
|
|
||
| > [!IMPORTANT] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,6 +204,7 @@ | |
| "type": "array", | ||
| "items": { | ||
| "enum": [ | ||
| ":percent", | ||
| "u:dir", | ||
| "u:id", | ||
| "u:locale" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| { | ||
| "$schema": "../../schemas/v0/tests.schema.json", | ||
| "scenario": "Percent function", | ||
| "description": "The built-in formatter and selector for percent values.", | ||
| "defaultTestProperties": { | ||
| "tags": [":percent"], | ||
| "bidiIsolation": "none", | ||
| "locale": "en-US", | ||
| "expErrors": [] | ||
| }, | ||
| "tests": [ | ||
| { | ||
| "src": "{:percent}", | ||
| "expErrors": [{ "type": "bad-operand" }], | ||
| "exp": "{:percent}" | ||
| }, | ||
| { | ||
| "src": "{foo :percent}", | ||
| "expErrors": [{ "type": "bad-operand" }], | ||
| "exp": "{|foo|}" | ||
| }, | ||
| { "src": "{1 :percent}" }, | ||
| { "src": ".local $n = {0.42 :number} {{{$n :percent}}}" }, | ||
| { "src": ".local $n = {42 :integer} {{{$n :percent}}}" }, | ||
| { "src": ".local $n = {0.01 :percent} {{{$n :percent}}}" }, | ||
| { "src": "{0.12345678 :percent}" }, | ||
| { "src": "{0.12345678 :percent maximumFractionDigits=1}" }, | ||
| { "src": "{0.12 :percent minimumFractionDigits=1}" }, | ||
| { "src": "{0.12 :percent minimumSignificantDigits=1}" }, | ||
| { | ||
| "src": "{$x :percent}", | ||
| "params": [{ "name": "x", "value": 0.99 }] | ||
| }, | ||
| { | ||
| "src": ".input {$n :percent} .match $n one {{one}} * {{other}}", | ||
| "params": [{ "name": "n", "value": 0.01 }], | ||
| "exp": "one" | ||
| }, | ||
| { | ||
| "src": ".input {$n :percent} .match $n one {{one}} * {{other}}", | ||
| "params": [{ "name": "n", "value": 1 }], | ||
| "exp": "other" | ||
| } | ||
| ] | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.