|
Post by danuthorincas on Apr 9, 2022 7:12:10 GMT
How can I rounddown or roundup the rezults ?
for i=1 to 10
print sqr(i)
next
'I want the result to be rounddown or roundup (depend how the result are :<=x.5 or >x.51)
'1.0 -----1
'1.41421356---1
'1.73205081---2
'2.0--2
'2.23606798--2
'2.44948974--2
'2.64575131--3
'2.82842712--3
'3.0--3
'3.16227766--3
|
|
|
Post by tenochtitlanuk on Apr 9, 2022 11:15:13 GMT
Try this...
for i=1 to 10 print sqr(i), using( "#.###", sqr( i)) next
PS note that code displays better ( as here) if you click the [C] button and paste in the indicated space.
|
|
|
Post by plus on Apr 9, 2022 17:11:25 GMT
Sorry .5 rounds up like .51 using Print using, 5's are commonly rounded up.
for i=.4 to .6 step .01 print i, using( "#", i) next
|
|
|
Post by tsh73 on Apr 10, 2022 6:05:09 GMT
as you see, int(x) in JB rounds towards zero here are user-defined functions ceil() - always round up floor() - always round down
As Bplus noted, this is unusual requirements. Usually .5 and above rounds up, anything less then .5 down. using() works like this. int(x+.5) on positive x works like this. Of cource it could be done - scroll below - but very likely you shouldn't really do that.
Then we debugged that calculator program what did beam loads (balances) we had function like round(number, decimalPlaces), but I was not able to find that thread.
x using(...) int(x) floor(x) ceil(x) -2 -2 -2 -2 -2 -1.75 -2 -1 -2 -1 -1.5 -2 -1 -2 -1 -1.25 -1 -1 -2 -1 -1 -1 -1 -1 -1 -0.75 -1 0 -1 0 -0.5 -1 0 -1 0 -0.25 0 0 -1 0 0 0 0 0 0 0.25 0 0 0 1 0.5 1 0 0 1 0.75 1 0 0 1 1 1 1 1 1 1.25 1 1 1 2 1.5 2 1 1 2 1.75 2 1 1 2 2 2 2 2 2
print "x", "using(...)", "int(x)", "floor(x)", "ceil(x)" for x = -2 to 2 step 0.25 print x, using("##",x), int(x), floor(x), ceil(x) next
'-------------------------------------------------- function floor(x) floor = int(x) floor = floor - (x <> floor and x < 0) end function
function ceil(x) ceil = int(x) ceil = ceil + (x <> ceil and x > 0) end function
Custom round function example - <.51 down, >=.51 up
for i=.4 to .6 step .01 print i, using( "#", i), customRound(i) next
function floor(x) floor = int(x) floor = floor - (x <> floor and x < 0) end function
function ceil(x) ceil = int(x) ceil = ceil + (x <> ceil and x > 0) end function
function customRound(x) 'I want the result to be rounddown or roundup (depend how the result are :<=x.5 or >x.51) frac = x mod 1 'tested on positive x if frac <.51 then customRound=floor(x) else customRound=ceil(x) end if end function
|
|