Scirra / Construct-feature-requests

A place to submit feature requests and suggestions for Construct.
https://www.construct.net
11 stars 1 forks source link

New expression truncate() #380

Open oosyrag opened 1 month ago

oosyrag commented 1 month ago

Reviewed guidelines

Checked for duplicate suggestions

Summary

Construct currently lacks an expression to round towards zero, which can be useful when dealing with negative numbers. Floor() and ceil() round towards negative and positive infinity. Int() sometimes serves this purpose in other programming languages, but behaves as floor() in construct when used on numbers.

Possible workarounds or alternatives

Flooring the absolute value of a number along with a conditional expression that could be used to identify if the number was originally negative to multiply the result by -1.

n<0?abs(floor(n))*-1:floor(n)

Edit: Or using JavaScript math.trunc() as described in the comments below

Proposed solution

A system expression trunc(n) or truncate(n) would remove the decimals or convert a float to an int, rounding towards 0 regardless of n being positive or negative.

Why is this idea important?

This expression would make rounding towards 0 more accessible and easier to implement for a beginner or otherwise not familiar with programming.

Additional remarks

Appreciate your consideration and thanks for all the work you put into Construct, as always.

F3der1co commented 1 month ago

Not to undermine the suggestion, just to give an option here is a workaround by doing it in js with a return function image

fodi commented 2 weeks ago

Wow, thanks for bringing attention to this, I had zero idea (no pun intended) that int(-0.1) returns -1 instead of 0. Interesting choice on Scirra's part, I would've expected it to work like parseInt in JavaScript which returns 0 for -0.1.