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

Get help using Construct 2

Post » Wed Jun 18, 2014 6:03 pm

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?
B
19
S
5
Posts: 80
Reputation: 1,383

Post » Wed Jun 18, 2014 6:12 pm

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.
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Thu Jun 19, 2014 1:10 pm

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.
B
19
S
5
Posts: 80
Reputation: 1,383

Post » Thu Jun 19, 2014 1:23 pm

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
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Mon Jun 23, 2014 11:38 am

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:

=(A-(((A/100)*B))/2)

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.
B
15
S
4
G
1
Posts: 286
Reputation: 1,988


Return to How do I....?

Who is online

Users browsing this forum: Baidu [Spider], sean080, Yahoo [Bot] and 18 guests