为什么Intl.NumberFormat无法在Safari和Firefox中与单元一起使用? [英] Why doesn't Intl.NumberFormat work with units in Safari and Firefox?

查看:128
本文介绍了为什么Intl.NumberFormat无法在Safari和Firefox中与单元一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用给出的示例

I'm trying to use the example given here concerning formatting a number.

console.log((16).toLocaleString('en-GB', {
    style: "unit",
    unit: "liter",
    unitDisplay: "long"
}));

我在chrome中尝试过,效果很好.但是,在Safari和Firefox中,我分别收到以下错误Error: style must be either "decimal", "percent", or "currency"Error: invalid value "unit" for option style.

I tried it in chrome and it works great. However, in Safari as well as Firefox I get the following errors respectively Error: style must be either "decimal", "percent", or "currency" and Error: invalid value "unit" for option style.

从文档中我发现,在查看浏览器兼容性之后,它应该可以在所有浏览器上运行.我试图寻找答案,但是找不到关于此问题的任何信息.有谁知道这是为什么,或者我可能在哪里找到更多信息?

From the docs I figured it should work on all browsers after looking at the browser compatibility. I tried looking for answers, but I can't find anything regarding this issue. Does anyone know why this is or where I could probably find more info?

推荐答案

问题来自style字段的值unit.

根据 ECMA-402,第六版,2019年6月 ECMAScript®2019国际化API规范:

According to ECMA-402, 6th edition, June 2019 ECMAScript® 2019 Internationalization API Specification:

此字段的值必须是一条记录,该记录必须包含带有三种数字格式样式的名称的字段:十进制",百分比"和货币".

The value of this field must be a record, which must have fields with the names of the three number format styles: "decimal", "percent", and "currency".

根据 ECMA-402草案/2020年2月27日 ECMAScript®2020国际化API规范:

According to the Draft ECMA-402 / February 27, 2020 ECMAScript® 2020 Internationalization API Specification:

此字段的值必须是一个Record,该字段必须包含具有四种数字格式样式的名称的字段:十进制",百分比",货币",和单位"

Firefox和Safari正在实施ECMA-402规范的第六版,而Chrome浏览器正在实施同一规范的草稿版本.规范草案可以随时更改,并且没有正式保证该新的unit值将包含在第7版中.如果您想跨浏览器兼容并且面向未来,则应坚持使用第6版,并等待第7版发布,然后再使用这些新功能.

Firefox and Safari are implementing the 6th edition of the ECMA-402 specification and Chrome is implementing the Draft version of this same specification. The draft specification can change at any time and there is no formal guarantees that this new unit value will be included in the 7th edition. If you want to be cross-browser compatible and future-proof, you should stick on the 6th edition and wait for the release of the 7th edition before using these new features.

如果您想了解详细信息,可以阅读此新功能的建议

If you want details you can read the proposition for this new feature.

这篇关于为什么Intl.NumberFormat无法在Safari和Firefox中与单元一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆