I am trying to localize my web application and I cannot manage to make Intl.NumberFormat work with electric units (ampere, ohm, volt, joule…).
In the documentation, they provide some examples and the list of units available.
Though I cannot manage to make it work with the electric units:
JavaScript
x
5
1
// Working
2
console.log(new Intl.NumberFormat('fr', { style: 'unit', unit: 'second' }).format(1000));
3
4
// Failing with Invalid unit argument for Intl.NumberFormat() 'volt'
5
console.log(new Intl.NumberFormat('fr', { style: 'unit', unit: 'volt' }).format(1000));
Does someone have an idea why?
Advertisement
Answer
A subset of units from the full list was selected for use in ECMAScript.
JavaScript
1
46
46
1
Simple Unit
2
-----------
3
acre
4
bit
5
byte
6
celsius
7
centimeter
8
day
9
degree
10
fahrenheit
11
fluid-ounce
12
foot
13
gallon
14
gigabit
15
gigabyte
16
gram
17
hectare
18
hour
19
inch
20
kilobit
21
kilobyte
22
kilogram
23
kilometer
24
liter
25
megabit
26
megabyte
27
meter
28
mile
29
mile-scandinavian
30
milliliter
31
millimeter
32
millisecond
33
minute
34
month
35
ounce
36
percent
37
petabyte
38
pound
39
second
40
stone
41
terabit
42
terabyte
43
week
44
yard
45
year
46
Pairs of simple units can be concatenated with “-per-” to make a compound unit. There is no default value; if the style is “unit”, the unit property must be provided.
Très cool: Megabytes per second becomes mégaoctets par seconde in French
JavaScript
1
5
1
console.log(
2
new Intl.NumberFormat('fr',
3
{ style: 'unit', unit: 'megabyte-per-second', 'unitDisplay': 'long' }
4
).format(1000)
5
);