How do I use .2f on Web Storage?

0 favourites
  • 3 posts
From the Asset Store
Basic Plugins for Firebase Authentication, Realtime-Database, Firestore, Cloud 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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • Think of it this way, if you have a number:

    3.14159265359

    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:

    314159.265359

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

    314159

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

    3.14159

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

    (round(3.14159265359*100000))/100000

    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)