如何根据 angular 2 中的用户区域设置格式化数字 [英] How to format number based on user locale in angular 2

查看:24
本文介绍了如何根据 angular 2 中的用户区域设置格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 angular 2 中根据用户区域设置格式化数字

How to format number based on user locale in angular 2

示例 - 如果用户区域设置为德语(德国),则数字显示为 1.234,56

Example - If the user locale is German (Germany) then the number is displayed as 1.234,56

推荐答案

JavaScript 中已经有一个可用的函数.你可以直接调用它.https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

There already is a function available in JavaScript. You can just call it. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

示例:

var number = 123456.789;

// German uses comma as decimal separator and period for thousands
console.log(number.toLocaleString('de-DE'));
// → 123.456,789

// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(number.toLocaleString('ar-EG'));
// → ١٢٣٤٥٦٫٧٨٩

// India uses thousands/lakh/crore separators
console.log(number.toLocaleString('en-IN'));
// → 1,23,456.789

// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(number.toLocaleString('zh-Hans-CN-u-nu-hanidec'));
// → 一二三,四五六.七八九

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(number.toLocaleString(['ban', 'id']));
// → 123.456,789

您可以将此区域设置存储在某个常量文件中 &玩它.

You can just store this locale's in some constant file & play around it.

这篇关于如何根据 angular 2 中的用户区域设置格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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