How do I display a number with a space every thousandth ?

Not favoritedFavorited Favorited 0 favourites
  • 4 posts
From the Asset Store
Add Number - Select match number to total required numbers
  • Hello,

    How do I display (in text object) a digit with a space at every thousandth like 1 000 or 1 000 000 ?

    Thanks !!

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If in event sheets, you'll have to declare and pass a local variable to a JS action. Let's say it's a string called num:

    let num = localVars.num;
    let string = "";
    for (let i = 1; i <= num.length; i++) {
    	string = num[num.length - i] + string;
    	if (i < num.length && ! (i % 3)) string = " " + string;
    };
    localVars.num = string;
    

    Then set your text to num. If it's not already a string, you'll have to convert it first.

    Basically, copy the string (number) starting from the last digit and adding a space every 3rd digit, unless it's the final (first) digit.

  • You don’t have to use js to do it. You can do it with events too. Here’s one way to do it that discards anything below the decimal point.

    Number n=1234567890
    String out=“”
    While
    — set out to (abs(n)<1000?int(n):(“ “&zeropad(int(abs(n)%1000),3)))&out
    — set n to n/1000
    — compare abs(n)<1
    — — stop loop

    Although if you want to use js for it you can utilize this one liner that groups the digits in threes for you. Or follow any convention of displaying numbers in your country of choice.

    new Intl.NumberFormat("en-US").format(number)

  • Thanks ! I find an other solution with ReplaceRegex().

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