typescript NativeScript:格式化数字打字稿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43325730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:26:17  来源:igfitidea点击:

NativeScript: Formatting number typescript

typescriptnativescript

提问by Ariel Capozzoli

I am totally new to nativescript and googling for the some basic stuff is harder than it should be... What i want to do is basically this: I have the number

我对 nativescript 完全陌生,谷歌搜索一些基本的东西比它应该的要难......我想做的基本上是这样的:我有号码

1234567.89

1234567.89

in a variable, and i want to show it in a label with the format

在一个变量中,我想将它显示在具有以下格式的标签中

"1,234,567.89"

“1,234,567.89”

Also, always show 2 decimals in case that 89 is 0 instead.

此外,如果 89 为 0,请始终显示 2 位小数。

Thanks for the help in advance

我在这里先向您的帮助表示感谢

回答by HolgerJeromin

If you target to modern browsers you can use:

如果您针对现代浏览器,您可以使用:

new Intl.NumberFormat('en-us', {minimumFractionDigits: 2}).format(1234567.89)

or

或者

(1234567.89).toLocaleString('en-us', {minimumFractionDigits: 2})

For browser support: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

浏览器支持:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

回答by Nick Iliev

TypeScript is a superset of JavaScript meaning that what you can do in JavaScript can be done in TS as well (but not the other way around). That said look at this article.

TypeScript 是 JavaScript 的超集,这意味着您可以在 JavaScript 中执行的操作也可以在 TS 中完成(但不能反过来)。那说看这篇文章

Now talking in the context of NativeScript, you can either provide the data to your binding context in the right format or use valueConverter.

现在讨论 NativeScript 的上下文,您可以以正确的格式向绑定上下文提供数据,也可以使用valueConverter

Hereyou can find examples on how to use value converters with data binding

在这里您可以找到有关如何将值转换器与数据绑定一起使用的示例