Search code examples
javascriptnumber-formatting

Exponent notation for Intl.NumberFormat


I'm trying to figure if there's a way for me to use Javascript's Intl.NumberFormat() for creating a square meter value with it.

Say, I would like to use it to create a value like 30 m² with that function. There are no exponent examples on the page or anywhere using Google. Is it even possible?


Solution

  • Unfortunately, while it's hard to prove a negative, I don't think you can do that. Intl.NumberFormat is (for now) limited to "sanctioned simple units" and "x per y" combinations of them as defined by the abstract IsSanctionedUnitIdentifier and IsWellFormedUnitIdentifier operations in the specification. You could do meters (for instance), or meters/hour or similar, but not square meters.

    Instead, you'll need your own formatting function (probably one that handles the numeric part via Intl.NumberFormat and then adds the part). It's tempting to use a formatter set to do meters and then just replace meter or m with meter² or , but you can't be sure that the text would have meter or m in it — which is probably most of the point of using Intl.NumberFormat. :-)