How do I round a float up to a certain decimal?

0 favourites
  • 5 posts
  • Hello,

    The round() function always round the number to integer. How do I round a number to a certain decimal?

    Something that would do this:

    1.23456789 --> 1.23

    1234 --> 1234.00

    123.456789 --> 123.45

    0.123456789 --> 0.12

    I tried various combinations of left(), round(), float() and zeropad() to no success.

    Is there another function that would do this?

  • round(yourvalue*100)/100 will give 2 decimal places.

    The number of 0's is the number of decimal places so round(yourvalue*10000)/10000 would be 4 decimal places.

  • Ah, so there isn't a single function to do this. Is there a way to ask Construct for this new feature? I mean a function where you give it a number and specify the decimal you want to keep. I'm pretty sure there's already a function like that in javascript. I guess it's in Math.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Pretty sure it wouldn't be that nessecary to add such a function due to it's simplicity (and since performance wise the result won't be problematic at all), you could use the function objec to do this if you think it is cleaner. Or if really you want to use the javascript function dedicated to that (if it even exists), you could use the execJS of the browser object, but I don t recommend it, just simply not worth the time

  • Ah, I was looking for something like this. I have a cooldown between spell attacks in my game, but I want an effect that reduces cooldowns by a percentage rather than a flat rate. The equation currently looks like this:


    where A is the base cooldown of the spell, and B is the cooldown bonus modifier (dividing by two at the end so that a 100% increase effectively halves the cooldown).

    My spell cooldowns count down in milliseconds, and I want the player to get the flat benefit of even a small percentage increase, so am I right in thinking that if I take the output of that equation and do floor(output*100)/100 that'll do the job?

    In other words, if a spell's cooldown is 0.30 seconds and the player has a 2% cooldown bonus, I want the cooldown to go to 0.29 seconds, rather than 0.297 which wouldn't provide any practical benefit.

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