Format Numbers

PortymanPortyman Member, PRO Posts: 409
edited November -1 in Tech Support
I am trying to format and display a stored integer with commas.

When i display text now, I get 45455454. I want to display 45,455,454. please help

Comments

  • rebumprebump Member Posts: 1,058
    I don't think there are any number formatting abilities in GS currently so you will have to roll your own (which is probably what you are asking since you probably have discovered there are none built in).

    Anyway, in this situation, division and modulus are your friends but it may get clunky.

    To get you thinking, if you have a number such as 12,345,678 you can break it apart with division and modulus:

    12,345,678 modulus 1,000 = 678 (note this number)
    12,345,678 - 678 = 12,345,000
    12,345,000 divided 1,000 = 12,345
    12,345 modulus 1,000 = 345 (note this number)
    12,345 - 345 = 12,000
    12,000 divided by 1,000 = 12 (note this number)

    Store those noted values for display. If the score doesn't update too rapidly, normal change attribute may suffice. If it changes rapidly (i.e. a shoot-em up), then some constrain attributes would be needed OR you can roll them all into one LONG expression (but some of the sub-expressions will repeat themselves as you will find out).

    One possible area of issue GS doesn't handle leading (or left padded) zeroes too well, so you may end up adding some rules for numbers such as 12,034,056 where the zeroes are at the leading edge of each number group (i.e. "034" or "056").
  • rebumprebump Member Posts: 1,058
    You will use concatenation too. In GS, it is the double-dot (period) operator:

    '$'..'345.00'

    GS handles spaces and leading zeroes oddly at times, so you may have to play around with them if you encounter them. Sometimes, you can work around the issue by storing the value in an attribute and using the attribute in the display text behavior.
  • PortymanPortyman Member, PRO Posts: 409
    Can you put some sample code to do this? I would really help.

    Thanks
  • rebumprebump Member Posts: 1,058
    The problem with doing this currently is the lack of the ability to easily left pad zeroes in any of the group of three numbers (i.e. the "023" in the number "14,023,443" or the "006" in the number "43,006"). Even if you break the number out using my method above, you then have to (currently) have a ruleset to pad out the left most zeroes if they are present. Add to that, you probably want this done real-time on a score that is constantly changing. (Side note: I heard there is a possible bug when multiple Rules are nested inside each other more than a few deep...but in this situation, you wouldn't be too deeply nested).

    What I am getting at is you are wasting processor resources in a game engine that can use all the resources it can get. I would finish your game without it, and if it performs very well on all devices (i.e. you get like 45+ frames per second on a 1st gen device), then think about implementing this and see if it degrades performance.

    The other issue is, you never know when the devs will add a text formatting feature that could easily supplant your contrived routine(s).
Sign In or Register to comment.