Decimals and zeros being shown

0 favourites
  • 13 posts
From the Asset Store
“Zero Numbers” is a logic game. Game with Source-Code (Construct 3 / .c3p) + HTML5 Exported
  • Hi there,

       I'm using this formula to show results with two decimals:

              round(N*100)/100

    But...

    How could I show numbers this way?----> '456.00' instead of just '456'

    Best regards!

  • It has to be a string to keep it displayed to two decimal points.

    int(N)&"."&zeropad(round((N-int(N))*100),2)

    This works.. There's probably a simpler way to do it though.

    I was gradually figuring it out and suddenly it started to look confusing as hell.

    It basically isolates the decimals, multiplies by 100 and zeropads it, before putting it back into a string with the original integer. But I'll explain it further if you want.

  • LOL keepee

    That is one long ass equation to add decimals points.

    I believe that is the best way to do it though.

  • A simpler (and yet, not really <img src="smileys/smiley17.gif" border="0" align="middle" />) way would be what you had, with a small addition:

    N = round(N)? (N&".00"):(round(N*100)/100)

    Using a fancy query thingy... It's actually not simpler at all, it's just a different kind of complicated. Simpler to ME because I use stuff like this all the time. <img src="smileys/smiley36.gif" border="0" align="middle" />

    If you've never seen this before, it's checking to see if there even ARE any decimals; if there aren't, just add a ".00" at the end. If there are, do what you originally do.

    (Edit: removed a pair of parentheses from the expression, just tested it and it gave a syntax error. Other than that it works fine)

  • Rory

    I know right? it's a bit crazy

    SullyTheStrange

    I completely forgot you could do queries within expressions so thanks for the reminder..!

    However that way won't work for when the number has one decimal place. 456.1 would still display 456.1 instead of 456.10

  • <img src="smileys/smiley32.gif" border="0" align="middle" />

    Keepee,your solution works great!! Thanks a lot friends!

    I had spent already some nights working out how to solve this issue without success.

    Two thumbs up!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ahhh yeah you're right keepee. I could make it even more needlessly complicated by checking for that too... or he could just use your method. Either way's fine by me. <img src="smileys/smiley36.gif" border="0" align="middle" />

  • Hi, I'll try to send you a sample of the code.

    Now, the next step I'm looking foward is to limit the possible text entries between 0 and 360.

    Do you think it's a good idea to create another global variable and then compare it to this values?

    Or perhaps try to find an expression that could solve the issue without adding more variables?

    I'll keep to find the solution!

    Any extra idea?

    Cheers!

  • In the next post you'll find a link to the project

  • project

  • You could use the min() and max() expressions. Min(x, y) returns whichever value is smaller, so min(360, 428) would return 360. Opposite for max, so max(0,-5) would return 0. Other methods might work better depending on how you have it set up (I'm on my phone right know, can't check the file myself)

  • Thanks a lot, man.

    When I have to pick up the value typed in the TextBox, I add this expression allowing me to avoid values under and over the range from 0 to 360.

    float(TextBox.Text)<=0? max(0,float(TextBox.Text)):min(360,float(TextBox.Text))

    I've used the expression that you have posted before, with this '?' and ':' that allows to program a condition with code directly on a line.

    And it works!!

    I find that this world of expressions gives a lot of power and flexibility to Construct2.

  • You could also use the clamp expression:

    clamp(float(TextBox.Text), 0, 360)

    Will return 0 if the value is below 0 and 360 if it's above 360.

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