Rounding Issue

0 favourites
  • 5 posts
  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

  • 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.

  • 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.

  • AllanR

    Ha, that's a great work around, works perfectly! Thanks for the idea.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)