# Display Currency Variable with Commas

Get help using Construct 2

### » Thu Feb 14, 2013 12:53 am

In my game players earn money which they can spend in the store, and the value they can earn get quite high so my problem is this. On screen I display how much money they have by pulling a value from a global variable and displaying it in a text box.

Currently it is displayed like this:
1234567.36

How I want it to look:
1,234,567.36

Basically I want a way for it to convert the number into a prettier version. This is my first project with Construct 2 and I've searched around as to how to do this but haven't found anything. Any help anyone can provide is greatly appreciated, thank you.
B
4
Posts: 5
Reputation: 635

### » Fri Sep 06, 2013 3:39 am

I know it's insanely late to post about this but I ran into the same question and couldn't find any simple solution... so I made one and thought it might help anyone who needs to do the same.

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

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

### » Mon Jun 09, 2014 5:20 pm

C2NumberFormatting.PNG

"FormatNumber"

RegexReplace(Function.Param(0) & "", "(\d)(?=(\d{3})+\$)", "g", "\$&,")

"FormatCurrency"

RegexReplace(Function.Param(0) & "", "\d(?=(\d{3})+\.)", "g", "\$&,")
You do not have the required permissions to view the files attached to this post.
B
23
S
6
Posts: 200
Reputation: 2,152

### » Wed Oct 22, 2014 2:05 pm

ggibson1 or anybody else that can answer. I have the FormatCurrency working. The problem i need help with is when I load the game there are no commas in any number until the number gets a decimal point in it. I have a button that costs 100k and the only way i have been able to get the commas to show is to *button cost by 1.11111 or some long number instead of just 1.12 that i want. As soon as the decimal place is added the commas appear. I don't understand RegexReplace enough to even try and figure this out on my own. I want to use this because all other examples i've found use way to many events just to get commas in numbers.
B
6
S
1
Posts: 36
Reputation: 452

### » Fri Aug 04, 2017 5:27 am

@ggibson1

Does RegexReplace... work with Sprite Fonts?

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