Rounding numbers? (Release Announcements)

by Aaron @, United States, Tuesday, November 19, 2013, 14:46 (1254 days ago)

Rounding numbers:

How do i get a number to be rouded up at .5 or higer and rounded down at .4 or less

I tried Ceil and Floor one rounds down absolutely and the other round up absolutely

I even tried using both calculated to a variable and the dived the two seperate results how ever this failed and still produced decimals sometimes. I am usually good at thinking outside the box, but this has me stumped. maybe it is in the manual and i missed the function?

is there a way to do it? if not my program i am making will be completely useless as it will not have correct values?

here is my program which is a calculation of miles per hour converted to meter per round(1 round = 12 seconds) its for a role playing game.

rem mph to combat round move meters. not correct redo
print " miles per hour "
input mph

mpr = mph * 5.36

mps = mpr
mpm = mps * 2
mpf = mpm * 2
mpvf = mpf * 2
print " Meters Per Round(Slow) " + mps
print " Meters Per Round(medium) " + mpm
print " Meters Per Round(fast) " + mpf
print " Meters Per Round(very fast) " + mpvf

maybe someone can help
Thanks in advance

Rounding numbers?

by Aaron @, United States, Tuesday, November 19, 2013, 14:50 (1254 days ago) @ Aaron

sorry here is the corrected program

rem mph to combat round move meters. not correct redo
print " miles per hour "
input mph

mpr = mph * 5.36

mps = mpr /8


mpm = mps * 2
mpf = mpm * 2
mpvf = mpf * 2
print " Meters Per Round(Slow) " + mps
print " Meters Per Round(medium) " + mpm
print " Meters Per Round(fast) " + mpf
print " Meters Per Round(very fast) " + mpvf
print "mpfl" + mpfl
print "mpc" + mpc

i need mps to be rounded down at .4 or less and rounded up at .5 or higher

Thanks in advance!

Rounding numbers?

by Aaron @, United States, Tuesday, November 19, 2013, 14:59 (1254 days ago) @ Aaron

Sorry i apologize i just realized i posted this in the wrong category!

Rounding numbers?

by Jim ⌂ @, Russell, KY, Wednesday, November 20, 2013, 01:24 (1253 days ago) @ Aaron

No problem - just be more careful next time.

I would create a round function and use it like this:

input "enter a number> ", i
print i + " rounded is " + round(i)


function round(n)
# round a number n
if n > 0 then
n = n + .5
return floor(n)
else
n = n - .5
return ceil(n)
end if
end function

Rounding numbers?

by Aaron @, United States, Wednesday, November 20, 2013, 07:33 (1253 days ago) @ Jim

Thanks Jim i appreciate the help. :)

Rounding numbers?

by mva1985 ⌂ @, Friday, January 03, 2014, 03:53 (1209 days ago) @ Aaron

if you know you will always be dealing with a positive number the easiest and fastest way is this:

int(x+.5)

example if x is 1.2 then it will return 1 BUT if x is 1.55 it will return a 2

RSS Feed of thread
powered by my little forum