0 Favourites

How do I use .2f on Web Storage?

  • We're trying to save the best score, but it's a float number. When we compile it we get a NaN because the number is too long. How do we save only 2 number after the comma? For actual result and best result?

  • floor(random(3)+1)

    Is an example from my .capx

    Random returns a float value but putting floor(value+1) makes it so it rounds down to the nearest whole value.

    Or you could try round(x) but I think both of those can only round to the nearest whole number and not any certain decimal place.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Think of it this way, if you have a number:


    If you want to trim down the precision of this floating point number, what you do is fairly simple.

    Let's say you want only 5 decimal places, you first multiply this number by 1*10^5, or, 100,000:


    Now you round it to the nearest whole number with the round operator.


    And finally divide it by that same 1*10^5 (100,000):


    And voila, you have trimmed off a few decimal places. You can even do this all in one handy equation:


    If someone could only clarify whether or not the float() operator needs to be used. This above is a pseudo implementation, but in theory that's exactly what you would do to trim your precision.

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