Display Number as Currency (With Commas)

Get help using Construct 2

Post » Sun Sep 16, 2012 8:48 pm

Does anyone know how to take a string of numbers:

68431351

And have it displayed as: 68,431,351

Basically add commas?

Thanks!
Epic Space Online - Google it!
B
34
S
10
G
5
Posts: 165
Reputation: 6,637

Post » Mon Sep 17, 2012 5:06 pm

Here is one way to do it:
http://dl.dropbox.com/u/5426011/examples14/format_number.capx

There are solutions to this very problem elsewhere on the forum but I cant find them.
B
79
S
24
G
52
Posts: 4,724
Reputation: 39,711

Post » Mon Sep 17, 2012 6:11 pm

I searched everywhere but I didn't have any luck. Thanks again R0J0 for the assist.

-T
Epic Space Online - Google it!
B
34
S
10
G
5
Posts: 165
Reputation: 6,637

Post » Sat Oct 13, 2012 8:42 pm

Weird, I input 1,000,000 and the comma doesn't show this time. Can you take a look?
Epic Space Online - Google it!
B
34
S
10
G
5
Posts: 165
Reputation: 6,637

Post » Sat Oct 13, 2012 9:48 pm

Hmm.. that's from a rounding error.

Here is a corrected capx without log10():
http://dl.dropbox.com/u/5426011/examples14/format_number2.capx
B
79
S
24
G
52
Posts: 4,724
Reputation: 39,711

Post » Fri Sep 06, 2013 3:42 am

Hey there! Yea, I know this is a dead thread but I had to implement it for a game and found a simple solution. If it helps anyone looking, cool!

I can whip up a capx if needed, but I think it'll be pretty simple to explain.

For example, if the number you need to display is in a variable 'cash', follow these steps:

1. Test to make sure the cash value is large enough

Ex: if floor(cash/1000) => 1

2. divide cash by 1000 and store the result in a separate variable like 'cash_thousands'

Ex: cash_thousands = floor(cash/1000)

3. subtract the thousands from the 'cash' value

Ex: subtract (cash_thousands * 1000) from cash

Note: it's probably wise to keep the original 'cash' value, the total for the game, in a safe spot and use temporary variables elsewhere.

4. pad the remaining hundreds and ones and store in a text variable (we'll call it 'cash_display').

Ex: set cash_display to cash_thousands & "," & zeropad(cash,3)

Note: you can also just display the number variables directly with the comma, if needed.

That's it. If you need bigger numbers, follow the same steps and make a cash_millions variable, dividing the original value by 1000000 and so on.

If I goofed up, please call me on it... but I think this way is pretty simple.

Thanks.
B
16
S
6
G
2
Posts: 152
Reputation: 3,764

Post » Tue Jan 28, 2014 1:31 am

Threads are never dead. This helped me out just today, and I like the solution Space Ape suggested. :) I didn't know about the zeropad function.

Thanks
B
15
S
5
Posts: 8
Reputation: 1,170

Post » Thu Nov 20, 2014 8:46 pm

Yep, still alive and kicking. I was having a go at doing this myself and just came across this. I was taking pretty much the same approach as Space Ape and this helped me confirm I'm approaching it right.
Image
B
9
S
3
G
1
Posts: 346
Reputation: 1,560

Post » Fri May 08, 2015 6:16 pm

Hello, I tried Ape's code and it turned out well. It's just that when my value updates on the Text Plugin, the transition every thousand "shakes the value on the string" everything's fine except for that.
B
26
S
14
G
3
Posts: 21
Reputation: 3,735

Post » Sat May 30, 2015 3:28 pm

BTW that capx above drops framerate from 60 to 14. I think because of the rapid looping it does to add commas.
Check out my games on Google Play:

BEAST ATTACK (500,000+ downloads!)

ASTROJUMP
B
34
S
8
G
1
Posts: 398
Reputation: 3,499

Next

Return to How do I....?

Who is online

Users browsing this forum: badmoodtaylor, Baidu [Spider] and 16 guests