Rounding Issue

Discussion and feedback on Construct 2

Post » Thu Jul 23, 2015 11:49 am

Hi,

I am incrementing an instance variable. When I come to display this value on the screen, Construct2 is showing a large number of decimal places that should not be there according to the calculation.

I attach a capx doc. In this example, I am incrementing the value 10 by 0.2, and had expected to see:

10
10.2
10.4
etc

Instead, the display is showing:

10
10.2
10.399999999999998
10.599999999999996
etc

Do I need to round to 2 decimal places each time? It feels I shouldn't need to do this kind of tidying up for such a simple calculation.

http://studioxenon.com/code/rounding_error/rounding_error.capx
B
4
Posts: 5
Reputation: 212

Post » Thu Jul 23, 2015 12:36 pm

how you tried, floor(nr) int(nr) or round(nr)? however they wont show you the decimals at all, but the 1st number, i.e 10.012321321 its going to be 10! 11.12312312321321 will be 11 and so on.. there is another way to do it, by eliminating the number of digits to show... there was a capx in the tutorials section ... its supposed to show from a number as 12.12321312 only the amount of digits you want as specified in the event sheet lets say you cant to show only the 2 digits after the . the number will be shown as 12.12 i think thats what you want... but forgot the link, its for sure about decimals search eliminating or removing decimals in tutorial section link
B
77
S
23
G
69
Posts: 1,353
Reputation: 43,960

Post » Thu Jul 23, 2015 1:02 pm

Results like that are common when dealing with floating point math.

In the line where you increase Health you can: Set Health to Round((Sprite.Health + 0.2)*10)/10
That will round to one decimal place.
B
97
S
34
G
20
Posts: 438
Reputation: 17,790

Post » Thu Jul 23, 2015 1:06 pm

Hi,

Thanks for responding. Yes, I did see that tutorial, I was just wondering if there was a better way of forming the calculation as it seems the source number is off (obviously not by much). It just seemed a bit weird to have to do this for 10 + 0.2.

If I get no other inspiration I will have to follow that string cleaning approach or re-think the whole idea.

Thanks anyway.
B
4
Posts: 5
Reputation: 212

Post » Thu Jul 23, 2015 1:10 pm

@AllanR

Ha, that's a great work around, works perfectly! Thanks for the idea.
B
4
Posts: 5
Reputation: 212


Return to Construct 2 General

Who is online

Users browsing this forum: olympo and 9 guests