NativeScript: Formatting number typescript

倾然丶 夕夏残阳落幕 提交于 2019-12-12 16:18:50

问题


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

1234567.89

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

"1,234,567.89"

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

Thanks for the help in advance


回答1:


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




回答2:


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.

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

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



来源:https://stackoverflow.com/questions/43325730/nativescript-formatting-number-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!