Round a variable
Posted: Wed Sep 09, 2009 10:41 pm
How can I round a variable? Does someone know a function for it that works in Algodoo?
scene.my.number >= (math.toint(scene.my.number) + 0.5) ? {scene.my.number = math.toint(scene.my.number) + 1} : {}; scene.my.number < (math.toint(scene.my.number) + 0.5) ? {scene.my.number = math.toint(scene.my.number)} : {}
KarateBrot wrote:yeah thanks that's nearly what i want! but is it possible to round to specific decimal places?
for example 6.490843697878903 will be rounded to 6.49084
standardtoaster wrote:KarateBrot wrote:yeah thanks that's nearly what i want! but is it possible to round to specific decimal places?
for example 6.490843697878903 will be rounded to 6.49084
To do that we would need to be able to truncate.
Scene.my.round = (x, digits) => { ( x*10^(digits + 1) - math.mod(x*10^(digits + 1), 1) ) >= ( (x*10^digits - math.mod(x*10^digits, 1) ) * 10 + 5 ) ? { ( (x*10^digits - math.mod(x*10^digits, 1)) + 1 ) / 10^digits } : { (x*10^digits - math.mod(x*10^digits, 1)) / 10^digits } }