{"action":"created","issue":{"url":"https://api.github.com/repos/tc39/ecma402/issues/32","labels_url":"https://api.github.com/repos/tc39/ecma402/issues/32/labels{/name}","comments_url":"https://api.github.com/repos/tc39/ecma402/issues/32/comments","events_url":"https://api.github.com/repos/tc39/ecma402/issues/32/events","html_url":"https://github.com/tc39/ecma402/issues/32","id":106675500,"number":32,"title":"Unit formatting API","user":{"login":"zbraniecki","id":449986,"avatar_url":"https://avatars.githubusercontent.com/u/449986?v=3","gravatar_id":"","url":"https://api.github.com/users/zbraniecki","html_url":"https://github.com/zbraniecki","followers_url":"https://api.github.com/users/zbraniecki/followers","following_url":"https://api.github.com/users/zbraniecki/following{/other_user}","gists_url":"https://api.github.com/users/zbraniecki/gists{/gist_id}","starred_url":"https://api.github.com/users/zbraniecki/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zbraniecki/subscriptions","organizations_url":"https://api.github.com/users/zbraniecki/orgs","repos_url":"https://api.github.com/users/zbraniecki/repos","events_url":"https://api.github.com/users/zbraniecki/events{/privacy}","received_events_url":"https://api.github.com/users/zbraniecki/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/tc39/ecma402/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-09-16T00:49:58Z","updated_at":"2015-09-22T17:43:44Z","closed_at":null,"body":"Unit formatting is similar to number formatting, but it involves one more units.\r\nThe goal is to produce strings like \"5 MB/s\", \"10 KB\", \"35 °C\" etc.\r\n\r\nI believe the most relevant for the Web audience areas would be:\r\n\r\n* Digital units\r\n* Duration units\r\n* Weather units\r\n* Length units\r\n* Compound units\r\n\r\nand maybe:\r\n* Energy units\r\n* Frequency units\r\n\r\nI would suggest that we limit ourselves first to just short version of units and use compound patterns to produce related units (like speed out of length and duration units).\r\n\r\nIn the future we may leave a window open to add 'variant' option for 'short'/'long' etc.\r\n\r\nProposed API:\r\n```javascript\r\nvar f = Intl.UnitFormat(navigator.languages, {\r\n  units: [\r\n    {type: 'digital', match: 'bestFit'}\r\n  ]\r\n});\r\nf.format(1024); // would return '1 KB' in en-US\r\n```\r\n\r\n```javascript\r\nvar f = Intl.UnitFormat(navigator.languages, {\r\n  units: {\r\n    {type: 'digital', unit: 'bestFit'},\r\n    {type: 'length', unit: 'second'}\r\n  }\r\n});\r\nf.format(1024); // would return '1 KB/s' in en-US\r\n```\r\n\r\n```javascript\r\nvar f = Intl.UnitFormat(navigator.languages, {\r\n  units: {\r\n    {type: 'length', unit: 'bestFit'},\r\n  }\r\n});\r\nf.format(2048); // would return '2 km' in en-US\r\n```\r\n\r\nOpen questions\r\n\r\n* Should that be a separate formatter or should we try to extend NumberFormat to handle units?\r\n* How to handle unitless formatting like '5/101' (use case: 5 out of 101 emails unread)\r\n* How to define reference unit in which the passed variable is in? Should we hardcode - meter for length, byte for digital? Or should we allow for it to be an option?\r\n* We should define some rounding strategy options"},"comment":{"url":"https://api.github.com/repos/tc39/ecma402/issues/comments/142360180","html_url":"https://github.com/tc39/ecma402/issues/32#issuecomment-142360180","issue_url":"https://api.github.com/repos/tc39/ecma402/issues/32","id":142360180,"user":{"login":"rxaviers","id":967155,"avatar_url":"https://avatars.githubusercontent.com/u/967155?v=3","gravatar_id":"","url":"https://api.github.com/users/rxaviers","html_url":"https://github.com/rxaviers","followers_url":"https://api.github.com/users/rxaviers/followers","following_url":"https://api.github.com/users/rxaviers/following{/other_user}","gists_url":"https://api.github.com/users/rxaviers/gists{/gist_id}","starred_url":"https://api.github.com/users/rxaviers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rxaviers/subscriptions","organizations_url":"https://api.github.com/users/rxaviers/orgs","repos_url":"https://api.github.com/users/rxaviers/repos","events_url":"https://api.github.com/users/rxaviers/events{/privacy}","received_events_url":"https://api.github.com/users/rxaviers/received_events","type":"User","site_admin":false},"created_at":"2015-09-22T17:43:44Z","updated_at":"2015-09-22T17:43:44Z","body":"An alternative API\r\n\r\n```js\r\nvar formatter = Intl.UnitFormat(locales, unit[, options]);\r\nformatter.format(value);\r\n\r\n// Example\r\nvar formatter = Intl.UnitFormat(\"en\", \"second\");\r\nformatter(2);\r\n// > \"2 seconds\"\r\n\r\nvar formatterShort = Intl.UnitFormat(\"en\", \"second\", {\r\n  form: \"short\"\r\n});\r\nformatterShort(2);\r\n// > \"2 sec\"\r\n\r\nvar speedFormatter = Intl.UnitFormat(\"en\", \"kilometer-per-hour\", {\r\n  form: \"short\"\r\n});\r\nspeedFormatter(100);\r\n// > \"100 kph\"\r\n\r\nvar speedFormatterPt = Intl.UnitFormat(\"pt\", \"kilometer-per-hour\", {\r\n  form: \"short\"\r\n});\r\nspeedFormatterPt(100);\r\n// > \"100 km/h\"\r\n```"}}