0 Favourites

Keep last decimal digit 0 of a variable

  • I was checking the below tutorial Decimals in a float and here is my workaround:

    Create the global variables:

    tNumber (text variable)

    fNumber (float variable)

    Later set tNumber = left( str(fNumber), find( str(fNumber), ".")+3)

    fNumber = 5003.563373

    tNumber = 5003.56

    fNumber = 78.30048573

    tNumber = 78.30   <-- last digit stays 0

    So what does the argument do?

    left(text, number) takes the text and returns number of characters

    The number of characters is set by find-function, which looks for the "." and adds the "." plus two additional characters (therefore 3 has to be added)

    fNumber = 5003.563373

    The dot "." is on position 4, but left()-function will return 5003

    Therefore we need to add 3 and this gives 5003.56 (7 characters in total).

    If variable *numberOfDecimals* (int) is required, simply define this variable.

    The formula is now:

    set tNumber = left( str(fNumber), find( str(fNumber), ".")+numberOfDecimals+1)

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Great solution!!

    I was looking for this like crazy

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