new Intl.NumberFormat([locales[, options]])
Intl.NumberFormat.call(this[, locales[, options]])…
- options
-
Optional. An object with some or all of the following properties:
-
…
- abbreviationThreshold
- Numbers whose absolute value is greater than this number will be abbreviated. Possible values are from
0toInfinity; the default isInfinity, which effectively disables abbreviation.
<dt>abbreviationRoundingMethod</dt> <dd>The rounding behavior to exhibit when abbreviating a number. Possible values are <code>Intl.NumberFormat.ROUND_CEILING</code>, <code>Intl.NumberFormat.ROUND_FLOOR</code>, <code>Intl.NumberFormat.ROUND_UP</code>, <code>Intl.NumberFormat.ROUND_DOWN</code>, <code>Intl.NumberFormat.ROUND_HALF_DOWN</code>, <code>Intl.NumberFormat.ROUND_HALF_EVEN</code>, and <code>Intl.NumberFormat.ROUND_HALF_UP</code>; the default is <code>Intl.NumberFormat.ROUND_HALF_CEILING</code>. For more information about rounding modes, see <a href="#roundingmodes">rounding modes</a>.</dd> … </dl>
…
…
The results can be customized using the options argument:
var number = 1234567.89;
…
// abbreviate a number
console.log(new Intl.NumberFormat('en-US', { abbreviationThreshold: 1e6, maximumSignificantDigits: 3 }).format(number));
// → 1.23M
console.log(new Intl.NumberFormat('en-US', { abbreviationThreshold: 1e6, style: 'currency', maximumSignificantDigits: 3 }).format(number));
// → $1.23M
console.log(new Intl.NumberFormat('ja-JP', { abbreviationThreshold: 1e6, maximumSignificantDigits: 3 }).format(number));
// → 123万
…- Intl.NumberFormat.ROUND_CEILING
- Rounding mode to round towards positive infinity.
- Intl.NumberFormat.ROUND_FLOOR
- Rounding mode to round towards negative infinity.
- Intl.NumberFormat.ROUND_DOWN
- Rounding mode to round towards zero.
- Intl.NumberFormat.ROUND_UP
- Rounding mode to round away from zero.
- Intl.NumberFormat.ROUND_HALF_CEILING
- Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards positive infinity.
- Intl.NumberFormat.ROUND_HALF_UP
- Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
- Intl.NumberFormat.ROUND_HALF_EVEN
- Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor.
- Intl.NumberFormat.ROUND_HALF_FLOOR
- Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards negative infinity.
- Intl.NumberFormat.ROUND_HALF_DOWN
- Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
(Prior art: http://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html)
I work on a product used by people who can follow other people. Follower counts can climb high enough that we must abbreviate them. When abbreviating them, we have two requirements:
This is a proposal for an abbreviation API for
Intl.NumberFormat. Adding anabbreviationThresholdoption and combining it with themaximumSignificantDigitsandmaximumFractionDigitsoptions would constitute a slam dunk for requirement 1. To satisfy the second, I need to be able to specify a rounding method.According to the usual rules of
Math.round, the English number would have been rounded to 7.7 – an overstatement of our follower count. By being able to specify a rounding mode, we can prevent this.Also, note that in some locales, such as
ja-JP, it's not typical to use the ‘millions’ abbreviation, but instead to talk about ‘765 ten thousands.’ Rolling abbreviation options and abbreviation rounding control intoIntl.NumberFormatwould make these feats of formatting possible with a really elegant API.