How do I append text to a string if the string does....

0 favourites
  • 6 posts
From the Asset Store
This collection of music is the beautiful performance of rather small strings ensemble (8 first violins, 6 second violin
  • ..not contain a decimal point?

    Basically I have a number. This could be 1 or 1.00 (or any number like this)

    If the number is 1 I want to append '.00' to it so ultimately they all look like the '1.00' format.

    I know I need to use append and the find(src, text) expression but I have no understanding of how to actually put them into contstruct in a logically formatted statement.

    Please help. Coming from traditional programming background a lot of Construct 2 seems a bit backwards to me

    for example....

    if(!myVar.Contains(".")){//do stuff}

    In Construct 2 however I cant figure out how to make the same statement.

    Thanks

  • So you need to convert a number to string with 2 decimal points?

    My method is a bit messy:

    d=round((myVar-int(myVar))*100)

    s=int(myVar) & "." & zeropad(d, 2)

    Or the same in one expression:

    Set s to int(myVar) & "." & zeropad(round((myVar-int(myVar))*100), 2)

    Maybe there is an easier way..

  • Okay thanks, but maybe this sounds stupid but how and where do I input that into Construct 2. What kind of event/condition/action? Thanks

  • If you want to save this in a string variable:

    New action -> System -> Set variable -> ...

    If you have a text object on your layout and want it to display this number:

    New action -> myText -> Set text -> .....

  • okay thanks!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can also use modulo 1 to get the fractional part. Also, int() behaves strangely with negative numbers.

    You can use abs() to change everything to positive and append a minus sign if needed using a conditional statement.

    (myVar < 0 ? "-" : "") & int(abs(myVar)) & "." & zeropad(round(abs((myVar%1)*100)),2)[/code:gkjyxtq4]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)