Random decimals added to numbers AS3

Pretty stupid thing I just tripped up on, but I was incrementing through a loop, adding 0.05 to an angle each time (for circular movement). I noticed after a while, the number, which should have just changed between 0.00, 0.05, 0.1 etc. was ending up being 0.15000000000000002 and therefore screwing everything up – for no apparent reason.

Not being a serious programmer, I’m vaguely aware that that’s because AS3 sends small decimals behind the scenes – beats me really but anyway, easy fix once you know, using the Number.toFixed() function.

myNumber = Number(myNumber.toFixed(4));

Maybe this’ll help some other muppit heh.

Tech Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑