有没有办法在JavaScript中通过Intl.NumberFormat反转格式 [英] Is there a way to reverse the formatting by Intl.NumberFormat in JavaScript

查看:430
本文介绍了有没有办法在JavaScript中通过Intl.NumberFormat反转格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Intl.NumberFormat (参见 Mozilla的文档)在Javascript中提供了一种很好的方式,可以将数字格式化为当前语言环境的版本,如下所示:

The Intl.NumberFormat (see Mozilla's doc) provides a nice way in Javascript to format numbers into a current locale`s version like this:

new Intl.NumberFormat().format(3400); // returns "3.400" for German locale

但是我找不到一种方法来反转这种格式。
是否有像

But I couldn't find a way to reverse this formatting. Is there something like

new Intl.NumberFormat().unformat("3.400"); // returns 3400 for German locale

感谢您的帮助。

推荐答案

我找到了一个解决方法:

I have found a workaround:

var separator = new Intl.NumberFormat().format(1111).replace(/1/g, '');
"3.400".replace(new RegExp('\\' + separator, 'g'), '');

不是最好的解决方案,但它有效: - )

Not the nicest solution but it works :-)

如果有人知道更好的方法,请随时发布您的答案。

If anyone knows a better way of achieving this, feel free to post your answer.

更新

这是一个完整的区域设备编号解析器,采用类似的方法。

Update
Here's a full working locale number parser with a similar approach.

这篇关于有没有办法在JavaScript中通过Intl.NumberFormat反转格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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