10

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
  • 1
    Just to add on to the answers below, this isn't a NativeScript question but a general javascript question (which includes typescript since it's a superset of js) Commented Apr 10, 2017 at 21:46

3 Answers 3

28

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, both suggestions and the link were what is was looking for!
4

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

Comments

0

In the NumberFormat options we can set the useGrouping indicator to true:

{ minimumFractionDigits: 2, useGrouping: true }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.