How do I round numbers to 1.1K, 2.5M, etc.?

Get help using Construct 2

Post » Sun May 07, 2017 12:00 am

I'm currently using the following to put commas in my numbers larger than 999:

RegexReplace(RapperDictionary.Get("RapFans") & "", "(\d)(?=(\d{3})+$)", "g", "$&,")

This is great, but in being a very novice student of Regular Expressions and programming in general, I'm trying to understand how I can flip what I have to make a number like 1123 turn into 1.1K. None of the tutorials I've read on Scirra.com or elsewhere really explain this, but attempt to explain RegEx as a whole system.

Obviously it's not just the number, but also the lettered value at the end of the string that needs to be dependent upon how many digits in the original string there are to properly kick out a K, M, B, etc.

Is this not a RegEx issue and perhaps something deeper like a Function?
B
14
S
5
Posts: 37
Reputation: 1,026

Post » Sun May 07, 2017 4:54 am

something like this should work:

value is less than a million and equal to or greater than 1000 >>> set text: round(Variable1/100)/10&"K"

off hand i'm not sure how to switch between k, m, b, ect without another event for each.
B
25
S
16
G
26
Posts: 125
Reputation: 16,103

Post » Sun May 07, 2017 9:27 am

@actuallybrown

Check my answer in this thread:
viewtopic.php?f=147&t=183178
Find all the Construct 2 games that were on Steam Greenlight here
B
35
S
9
G
7
Posts: 467
Reputation: 6,864

Post » Sun May 07, 2017 6:49 pm

I once did something similar. It used a function and an array to store abbreviation values. You can expand it as you like, the system will stop using abbreviation when there aren't any to represent the number with less than 3 digits and fallback to something like 324e75. Construct's expression editor can't handle large numbers so I had to get creative, you'll see what I'm talking about.

I also had to squeeze in an ExecJs somewhere so I could check for infinity, C2 simply lack this feature.

https://www.dropbox.com/s/ry1ry2wbxmycd ... .capx?dl=0
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Post » Mon May 08, 2017 10:10 am

Thanks all. I think I'm going to go with @celstrider 's recommendation; it seems the easiest for my system.
B
14
S
5
Posts: 37
Reputation: 1,026


Return to How do I....?

Who is online

Users browsing this forum: Artcadev, newt and 6 guests