[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,566

Post » Wed Mar 11, 2015 12:45 am

Image
B
57
S
23
G
7
Posts: 1,435
Reputation: 10,524

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
B
63
S
16
G
63
Posts: 2,170
Reputation: 39,985

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,566

Post » Wed Mar 11, 2015 4:03 pm

Yes..Ceil(number) - Round a number up.. ceil(-32.7023072454) = -32 ...................... (-32 > -33 :D )
Image
B
57
S
23
G
7
Posts: 1,435
Reputation: 10,524

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,566

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
83
S
27
G
69
Posts: 4,905
Reputation: 48,977

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,566

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,566


Return to How do I....?

Who is online

Users browsing this forum: GamerGon, lamar, R0J0hound, UncleHo74 and 2 guests