How do I make a string of numbers have commas?

Get help using Construct 2

Post » Wed Aug 09, 2017 5:37 am

I want to find out how to add commas to a string of numbers, ex. 1234567890 to 1,234,567,890. I saw a previous thread on this but the link to the solution was broken.
B
12
S
6
Posts: 5
Reputation: 1,012

Post » Wed Aug 09, 2017 12:08 pm

You need to add comma then ;) and some expression >>> https://www.dropbox.com/s/vsuh3hd8k6yd6 ... .capx?dl=0
ImageImage
B
105
S
46
G
23
Posts: 778
Reputation: 22,226

Post » Wed Aug 09, 2017 5:42 pm

Message: CutePichu can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
alextro wrote:You need to add comma then ;) and some expression >>> https://www.dropbox.com/s/vsuh3hd8k6yd6 ... .capx?dl=0


The code works well for numbers 1,000 and 1,000,000 but the code adds an extra comma at the end of numbers like 100,000. Also, the code doesn't work well with numbers less than 1,000. Constantly adding unnecessary commas everywhere.
B
12
S
6
Posts: 5
Reputation: 1,012

Post » Wed Aug 09, 2017 6:09 pm

The following regular expression will match positions which need a comma inserted.
Code: Select all
(\d)(?=(\d{3})+$)

So the following RegexReplace should do the trick :
Code: Select all
RegexReplace(str(value), "(\d)(?=(\d{3})+$)", "g", "$1,")

Note that it will only work with integers.
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Wed Aug 09, 2017 6:30 pm

Magistross wrote:The following regular expression will match positions which need a comma inserted.
Code: Select all
(\d)(?=(\d{3})+$)

So the following RegexReplace should do the trick :
Code: Select all
RegexReplace(str(value), "(\d)(?=(\d{3})+$)", "g", "$1,")

Note that it will only work with integers.


Thanks, the code works perfectly.
B
12
S
6
Posts: 5
Reputation: 1,012

Post » Thu Aug 10, 2017 6:20 am

I revised and updated the example. Now it should show correct display >>> https://www.dropbox.com/s/vsuh3hd8k6yd6 ... .capx?dl=0
ImageImage
B
105
S
46
G
23
Posts: 778
Reputation: 22,226

Post » Thu Aug 10, 2017 9:07 pm

alextro wrote:I revised and updated the example. Now it should show correct display >>> https://www.dropbox.com/s/vsuh3hd8k6yd6 ... .capx?dl=0


The code works a bit better but its still broken with numbers less than 100, like 10 or 1.
B
12
S
6
Posts: 5
Reputation: 1,012

Post » Thu Aug 10, 2017 10:55 pm

So add another condition so that it only does the comma parse if the number is bigger than 100 ;)
Image



Coming soon on Steam!
B
16
S
9
G
3
Posts: 204
Reputation: 3,466

Post » Fri Aug 11, 2017 12:47 am

As suggested by @mikehive the capx is fixed now. Check it again.
ImageImage
B
105
S
46
G
23
Posts: 778
Reputation: 22,226


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, Google [Bot], Nicolas951, tarek2 and 13 guests