Display Currency Variable with Commas

0 favourites
  • 5 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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.

  • 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.

  • [attachment=0:35rsixyn][/attachment:35rsixyn]

    "FormatNumber"

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

    "FormatCurrency"

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

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • ggibson1

    Does RegexReplace... work with Sprite Fonts?

    Thanx!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)