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
115
S
51
G
24
Posts: 790
Reputation: 23,505

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
76
S
31
G
27
Posts: 1,015
Reputation: 20,790

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
115
S
51
G
24
Posts: 790
Reputation: 23,505

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: 209
Reputation: 3,476

Post » Fri Aug 11, 2017 12:47 am

As suggested by @mikehive the capx is fixed now. Check it again.
ImageImage
B
115
S
51
G
24
Posts: 790
Reputation: 23,505


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 26 guests