How do I get int separated with comma?

Get help using Construct 2

Post » Sat Aug 22, 2015 8:57 pm

Hi! I have small problem and question. How to get string in style: 123,456,789 from int: 123456789?
ps. Sorry for mistakes :P
B
6
S
1
Posts: 25
Reputation: 525

Post » Sat Aug 22, 2015 9:28 pm

Variable1=123456789
set Text............................ left(str(Variable1),3)&","&mid(str(Variable1),3,3)&","&right(str(Variable1),3)
B
67
S
24
G
7
Posts: 1,518
Reputation: 11,072

Post » Sat Aug 22, 2015 10:06 pm

Thanks :D
Works for me:
Code: Select all
left(str(var0),1) & "," & mid(str(var0),2,3) & "," & mid(str(var0),5,3) & "," & mid(str(var0),7,3)
B
6
S
1
Posts: 25
Reputation: 525

Post » Sat Aug 22, 2015 10:12 pm

I wouldn't recommend that solution, as it only works for numbers with a fixed number of digits. I'd suggest using RegexReplace instead, demonstrated by the following expression:

Code: Select all
RegexReplace(str(MyNumber), "\B(?=(\d{3})+(?!\d))", "g", ",")

Regular expression comes from a similar question on StackOverflow.
Moderator
B
113
S
41
G
20
Posts: 1,586
Reputation: 19,126


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 22 guests