Add commas into score/number

Get help using Construct 2

Post » Wed Jun 18, 2014 6:47 pm

I checked your CAPX. I noticed the number you want to format has a floating point. My Regex assume you don't. The "FormatCurrency" Regex assume there is a floating point. So you might have to combine both in a function and use the appropriate Regex. Be sure to pass the value as a string to the regex, otherwise it might behave strangely. You can do that by appending an empty string to the variable (implicit conversion), or using the str() function (explicit conversion).

Here's the modified CAPX.

@tomhaiger You might want to read a bit on "regular expressions" in general, especially the Javascript variant, which lacks the lookbehind functionnalities. It's quite a powerful feature for matching and replacing strings.
You do not have the required permissions to view the files attached to this post.
B
76
S
31
G
27
Posts: 1,015
Reputation: 20,790

Post » Wed Jun 18, 2014 8:56 pm

i edited my previous post, after adding int(number) it worked fine, and for the currency int(number*100)/100. Thanks for the tips, much smaller code than i was using.
B
6
S
1
Posts: 51
Reputation: 502

Post » Thu Jun 19, 2014 12:14 am

tomhaiger wrote:I'm a beginner here and I too was looking at how to insert commas into numbers. Thanks @trultz for the detailed guide into doing this.

I'm curious to @Magistross & @ggibson1 method of doing this. Could you guys go into a bit more detail on how to use this expression please? I've tried inputting this into my score via functions but its not worked for me as nothing happens.

I haven't found many topics that talk about RegexReplace so my knowledge on the matter isn't great, which is probably why it hasn't worked for me.

Thanks.


FormatNumberExample.capx
You do not have the required permissions to view the files attached to this post.
B
21
S
5
Posts: 196
Reputation: 1,984

Post » Thu Jun 19, 2014 2:45 pm

I've got this working with my scores during gameplay so thanks for that but I've got an issue trying to get this to work with my webstorage.

Basically when i try to input this into my webstorage event which saves and displays highscores, it works the very first time after reseting webstorage value but after that the score doesn't update regardless if the score has been beaten until i reset the webstorage value again.

I've provided a screenshot of what i've done, i hope this is enough to go by

Image

Sorry if i missed out anything out, hopefully someone can help.
B
5
S
2
Posts: 126
Reputation: 876

Post » Fri Jun 20, 2014 2:17 am

tomhaiger wrote:I've got this working with my scores during gameplay so thanks for that but I've got an issue trying to get this to work with my webstorage.
Sorry if i missed out anything out, hopefully someone can help.


What I normally do to quickly find the bug in my code is use a Browser.Log and write out the variable in question or the return value of a function...etc. until I see what is going wrong.
B
21
S
5
Posts: 196
Reputation: 1,984

Post » Fri Jun 20, 2014 3:47 pm

I don't think it's a bug because if i change the Webstorage action back to how it was before it works the way it should do, the highscore updates when the score is beaten and stays that way until it's beaten again.

Heres how the web action was before -

Webstorage - Set local key "Highscore" to round(CurrentDistance)

It's only when i change it to how it is in the picture above that the problem arises. So i think its a problem with how i've done it, but i'm not sure what i've done wrong. Do i have to do something different with the expression because i'm using Webstorage?
B
5
S
2
Posts: 126
Reputation: 876

Post » Fri Jun 20, 2014 4:16 pm

Don't save the formatted value. And change the event at line 8 (from your picture above) so you format the value from the LocalStorage. You have to store the decimal value, not a string representation.

Be wary when you compare values, because "1000" is lesser than "200", but 1000 is greater than 200.
B
76
S
31
G
27
Posts: 1,015
Reputation: 20,790

Post » Fri Jun 20, 2014 4:58 pm

Thanks! That's fixed the problem :)
B
5
S
2
Posts: 126
Reputation: 876

Post » Wed Dec 10, 2014 8:06 pm

I have been using trultz example for currency (comma4). I don't have any idea how to make the commas work past trillions though. I'd like to get it to work until it hits scientific equations if possible. Anyone out there able to help me out please
B
6
S
1
Posts: 36
Reputation: 452

Post » Fri Aug 04, 2017 5:28 am

@Magistross

Does RegexReplace... work with Sprite Fonts?

Thanx!
B
3
S
1
Posts: 6
Reputation: 287

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: dop2000 and 25 guests