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
0
toInfinity
; 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)
Notes...
Where do these come from? (Neither are defined in http://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html)
Presumably the const integer value of these rounding modes are the same as
java.math.roundingModes
? If so, thenUNNECESSARY
should exist with a value of7
before any additional modes are defined.Furthermore, I would specify as a frozen object with a
null
prototype (that is either defined as the value of a static property onIntl
or defined as part of a standard library module).Defining them like this affords nicer usage:
or...
*
abbreviationRoundingMethod
is justroundingMode
to avoid unnecessary confusion.