Format high numbers for readability?

  • I couldn't really find what i was looking for.

    I have some values stored in variables that should be shown in text objects. these are pretty high numbers.

    Examples of things i want to do:

    Format the values

    25700000 to ----> 25,700,000 and 25,7 million

    267300 ti 267,000 or 267 thousand.

    I'm quite amazed that Construct still doesn't have any built in support for handling and displaying values in a good way. Usually Requires very complex expressions to get it right.

    Anyone have any good example or formula for displaying integers with commas for every 3 digit?

    1,000 to 1,000,000,000 or higher?

  • Made a solution with a function that is pretty flexible. It took a bit of time but this function i can just paste in any project, and feed it any number and it will spit out correct number format.

    Sharing if anyone finds it useful.

    dropbox.com/s/o1pcct9quz4mgsu/numberconvert2.c3p

  • This question comes up a lot. There are many solutions, but the forum search doesn't work well enough to find them easily.

    The most recent has a nice regex formula by dop2000

    construct.net/forum/construct-2/how-do-i-18/number-with-space-136985

  • R0J0hound for some reason the regex does not work in c3, or maybe just firefox?

  • Admittedly I didn't test it. Regex should work everywhere. I know there are other solutions too. Personally I've done it with a loop by either using some math to extract 3 digits at a time, or use one of the text expressions to get three characters at a time.

    It's hard to search though. I find I do everything from scratch anyways when using Construct. You have something that suits your purpose. Carry on.

  • Here is what I learn from other thread (forgot which one.. I cannot find the thread anymore)

    so I make this new example. maybe this could simplify it a lil bit

    object , events, result :

  • Ahh nice. That looks a bit easier too read than my example. I'm gonna see if i can recreate something similar in to a function like my earlier example so it's easy to reuse in projects, and maybe do something similar for time. converting seconds to:

    hh:mm:ss:ms format

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I couldn't really find what i was looking for.

    I have some values stored in variables that should be shown in text objects. these are pretty high numbers.

    Examples of things i want to do:

    Format the values

    25700000 to ----> 25,700,000 and 25,7 million

    267300 ti 267,000 or 267 thousand.

    I'm quite amazed that Construct still doesn't have any built in support for handling and displaying values in a good way. Usually Requires very complex expressions to get it right.

    Anyone have any good example or formula for displaying integers with commas for every 3 digit?

    1,000 to 1,000,000,000 or higher?

    This works in firefox

    RegexReplace(str(Your_variable_here), "(\d)(?=(\d{3})+$)", "g", ",")

    Edit, just noticed it needs a little more adjusting

  • Ok I have finally got a regex that works in Firefox

    RegexReplace(str(Variable1), "\d{1,3}(?=(\d{3})+(?!\d))", "g", "$&,")

    working example below

    https://www.sizzle-games.com/bignumbers/

  • sizcoz Ahhh that's great. Makes my life a lot easier. Thanks a lot.

    I think I need read up on regex to figure out what those things do, so I can make use of it in the future.

    The good thing with events like previous examples with loops, are that they are a lot easier to read/customize and understand. Regex is nice and short, but looks like the cat just stepped on the keyboard. :D

  • sizcoz Ahhh that's great. Makes my life a lot easier. Thanks a lot.

    I think I need read up on regex to figure out what those things do, so I can make use of it in the future.

    The good thing with events like previous examples with loops, are that they are a lot easier to read/customize and understand. Regex is nice and short, but looks like the cat just stepped on the keyboard. :D

    I agree.

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