[Solved] How do I get an int() without rounding up?

Get help using Construct 2

Post » Wed Mar 11, 2015 12:02 am

int(-32.7023072454) = -33

floor(-32.7023072454) also = -33

How can I get just the -32 integer part?

Thanks :-)
Last edited by locohost on Thu Mar 12, 2015 10:54 am, edited 1 time in total.
B
13
S
4
Posts: 280
Reputation: 1,578

Post » Wed Mar 11, 2015 12:45 am

B
66
S
24
G
7
Posts: 1,514
Reputation: 11,039

Post » Wed Mar 11, 2015 1:09 am

To work with positive and negative values: the integer of a variable Var = int(abs(Var)) * abs(Var) / Var
A big fan of JavaScript.
B
74
S
20
G
71
Posts: 2,228
Reputation: 44,888

Post » Wed Mar 11, 2015 12:27 pm

korbaach wrote:ceil(-32.7023072454) = -32 :)

https://www.scirra.com/tutorials/77/nat ... onstruct-2


Umm, thanks, but click the link you provided...

Ceil(number) - Round a number up (eg. ceil(6.1) returns 7).
B
13
S
4
Posts: 280
Reputation: 1,578

Post » Wed Mar 11, 2015 4:03 pm

Yes..Ceil(number) - Round a number up.. ceil(-32.7023072454) = -32 ...................... (-32 > -33 :D )
B
66
S
24
G
7
Posts: 1,514
Reputation: 11,039

Post » Wed Mar 11, 2015 4:42 pm

int(), floor(), ceil() all round up. Not sure why.

For now I'm using this string parser/converting/thing, calling it "Trunc". It's working...

C2TruncateFunction.PNG
You do not have the required permissions to view the files attached to this post.
B
13
S
4
Posts: 280
Reputation: 1,578

Post » Wed Mar 11, 2015 6:16 pm

int() and floor() round down to negative infinity. -33 is less than -32.7023072454 so that's down.
int(-32.7023072454) = -33
floor(-32.7023072454) = -33

ceil() rounds up to positive infinity.
int(-32.7023072454) = -32

round() rounds to whatever direction is closer.
round(-32.7023072454) = -33
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993

Post » Wed Mar 11, 2015 6:30 pm

Hmmm... Ok you're right. Perhaps using the negative number in the OP is making this harder to understand :?
B
13
S
4
Posts: 280
Reputation: 1,578

Post » Thu Mar 12, 2015 10:59 am

Yeah you guys are right, it's easy. I used a ternary operator to basically perform a trunc()...

Code: Select all
 decimalAngle = ( decimalAngle < 0 ) ? ceil(decimalAngle) : floor(decimalAngle)

This works. Thank you everyone! :-)
B
13
S
4
Posts: 280
Reputation: 1,578


Return to How do I....?

Who is online

Users browsing this forum: beohs, divsyntax, tunepunk and 6 guests