Skip to content

Issue with "Intl.NumberFormat.prototype.formatToParts()": type: 'compact' is missing #516

@mjpieters

Description

@mjpieters

MDN URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/formatToParts

What information was incorrect, unhelpful, or incomplete?

The list of 'possible types' is incomplete, when using Intl.NumberFormat({notation: 'compact'}).formatToParts(number) the magnitude of the value is passed in as a part with type: 'compact'

Specific section or headline?

Description, under the Possible types are the following: list.

What did you expect to see?

`'compact' is missing in this list, as per EMCA 402, section 12.1.7 (PartitionNotationSubPattern), step 4. - c. - iv. - 2.:

iv. Else if p is equal to "compactSymbol", then

  1. [...]
  2. Append a new Record { [[Type]]: "compact", [[Value]]: compactSymbol } as the last element of result.

as well as step 4. - c. - v. - 2.:

v. Else if p is equal to "compactName", then

  1. [...]
  2. Append a new Record { [[Type]]: "compact", [[Value]]: compactName } as the last element of result.

Did you test this? If so, how?

JavaScript code using the part type:

const compactFormat = Intl.NumberFormat('en', {
    notation: 'compact',
    compactDisplay: 'short',
    style: 'decimal',
    maximumFractionDigits: 3,
});

// formatValue(42174812) => {formatted: '42.175', magnitude: 'M'}
function formatValue(value) {
    let magnitude = '';
    return {
        formatted: compactFormat
            .formatToParts(value)
            .reduce((formatted, part) => {
            switch (part.type) {
                case 'compact':
                    magnitude = magnitude + part.value;
                    break;
                default:
                    formatted = formatted + part.value;
            }
            return formatted;
        }, ''),
        magnitude: magnitude
    };
}

console.log(formatValue(42174812));

outputs

Object { formatted: "42.175", magnitude: "M" }
MDN Content page report details

Metadata

Metadata

Assignees

Labels

Content:JSJavaScript docseffort: mediumThis task is a medium effort.help wantedIf you know something about this topic, we would love your help!

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions