Display Number as Currency (With Commas)

0 favourites
From the Asset Store
Jump on numbers in the proper order and reach the sun!
  • Does anyone know how to take a string of numbers:

    68431351

    And have it displayed as: 68,431,351

    Basically add commas?

    Thanks!

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

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

    -T

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Weird, I input 1,000,000 and the comma doesn't show this time. Can you take a look?

  • Hmm.. that's from a rounding error.

    Here is a corrected capx without log10():

    http://dl.dropbox.com/u/5426011/examples14/format_number2.capx

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

  • 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

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

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

  • BTW that capx above drops framerate from 60 to 14. I think because of the rapid looping it does to add commas.

  • May not be relevant anymore, and may not be very good practice but I did this using string functions and created a function out of it. Seems to perform extremely well. I have this on my site, if you could recommend somewhere else to host it that would be great.

    http://www.codersonline.net/C2/NumberFormat.zip

  • Figured I would share what I wound up with after looking into it a bit more. I'm not sure how it performs compared to the others, but it's working well for my use I guess.I have a one-liner javascript call that I use when I need to do this for websites, so I used the Construct Browser.ExecJS to it.

    dl.dropboxusercontent.com/u/71308871/c2-formatnumber/FormatNumber.caproj

    Browser.ExecJS("parseInt(" & Function.Param(0) & ", 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,').replace('.00','');")[/code:2onwbtac]
    
    The regex in there requires a decimal, hence the toFixed. My usage is usually with whole numbers so I remove the addition of the decimal points at the end with the additional replace('.00','') call.
  • Hi guys, I´m not sure if this is a good place to ask, but does anyone know a good way to show 1.5 k gold intead of 1500 gold? And change abbreviation as the number goes higher? Also I don´t know exactly how to search this on google, I searched number abbreviation and found this tread.Thanks!

  • Hi guys, I´m not sure if this is a good place to ask, but does anyone know a good way to show 1.5 k gold intead of 1500 gold? And change abbreviation as the number goes higher? Also I don´t know exactly how to search this on google, I searched number abbreviation and found this tread.Thanks!

    Let's say you have a variable called GOLD. What I do now, is...

    event (If GOLD > 999 & < 10000)

    Action (Set text to left(str(GOLD),1))&"."&mid(str(GOLD),1,1))

    Something like that. Using lefts and mids is a lot easier than having multiple functions and variables.

  • AnD4D thanks for the help, it worked really well. But in a case where the player is accumulating 1000 gold per second, and he has already accumulated over 1 million gold, the "1,20" format doesnt show increase very often due to the income being too small. Do you know a way around this issue, without having to use more decimal points? I have limited space to work with. thanks!

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