Round() Command missing?
Blitz3D Forums/Blitz3D Programming/Round() Command missing?
| ||
Am i blind? I cannot find any command that can round Floats to a special accuracy like: float#=1.23523452 newfloat#=Round(float#,3) newfloat# will now be 1.235 I know, i could write a function which will do the job - but i am not willing to always code-around missing basic commands. i think such a command is essential and should be integrated by mark. or am i wrong? |
| ||
I can't think of a highly practical uses for it You could always do: num$="1.2345" Print Left(num$,4) this would print "1.23" on the screen |
| ||
yeah but it does not round it up correctly. i have lots of practical uses for it (especially for my game). |
| ||
Play with this:Function Round#(Value#,Digits) Local Newval# if digits>1 then newval#=int(value#*(10^(digits-1)))/(10^(digits-1)) else newval#=int(value#) endif Return newval End Function [Edit] Untested! |
| ||
Function Round#(val#,precision) val#=Floor#(val# * 10^precision) val#=val/10^precision return val# end function Same code using Int to round instead of truncate: Function Round#(val#,precision) If precision=0 Then Return Int(val#) Else val#=Int(val# * 10^precision) val#=val/10^precision Return val# End If End Function |
| ||
kevin, your code does not round correctly. cygnus, thanks! (though you have a little typo in your code) |
| ||
true, my code truncates at whatever precision you ask for. |
| ||
@Cygnus: Noticed that too Val to value# Still, think it'll work though |
| ||
Hi, I agree this should be built in to blitz. I'll add this to the code archives. 'Examples: Local a:Float = 11.62552 Print "Rounding "+a Print round(a,5) 'nearest mutiple of 5 Print round(a,2) 'nearest even number Print round(a,1) 'nearest int Print round(a,.1) 'one decimal place Print round(a,.001) 'three decimal places Function Round:Float(num:Float,precision:Float) Return Int(num/precision+.5*Sgn(num))*precision EndFunction |
| ||
yeah, Val should have been Val#. I typed it straight into the post window, as i said, it would need playing with :) Kevin- your code is functionaly the same as mine, but does it work when you round to one place? (to me, one place means the INT value, two places is INT plus one of the following floats. You also need to use INT instead of FLOOR :) That should do what he wants. [edit] I updated the code. |
| ||
it works from precision of 0 on upwards...truncates, though. |
| ||
@Cygnus. Shouldn't it be..Function Round#(value#,Digits) |
| ||
Surely it would have to return a string? Floating point numbers aren't stored to x digits are they? |
| ||
Hahaha! Yes thats right :) I fixed it, must not have posted the change! Kevin, thats cool. Your code should be more efficient than mine then. I programmed my code to accept 1 to be the integer part and 2 to be after the floating point etc. Still, you should use INT instead of FLOOR to get it to round correctly. |
| ||
Floor and Ceil commands round up or down. |
| ||
just a small alteration this returns a value rounded to the number of decimal places entered Function Round#(Value#,Digits) Local Newval# If digits>1 Then newval#=Int(value#*(10^(digits))+0.5)/(10^(digits)) Else newval#=Int(value#) EndIf Return newval End Function |
| ||
This is the one I use, and after test, it seams to be the fastest way. => (*) is faster than (/) and x*x is faster than x^n so, it's optimised to run fast with lot of uses. fl#=1.2345678901 For n=0 To 10 Print LIB_GET_Decimal(fl,n) Next WaitKey End Function LIB_GET_Decimal#(Var#,Dec%=0) Local l_D#,l_f# Select Dec Case 0 l_D=1 l_f=1 Case 1 l_D=10 l_f=.1 Case 2 l_D=100 l_f=.01 Case 3 l_D=1000 l_f=.001 Case 4 l_D=10000 l_f=.0001 Case 5 l_D=100000 l_f=.00001 Case 6 l_D=1000000 l_f=.000001 Case 7 l_D=10000000 l_f=.0000001 Case 8 l_D=100000000 l_f=.00000001 Case 9 l_D=1000000000 l_f=.000000001 Default Return Var End Select v2% = Var*l_D Var = Float(v2)*l_f Return Var End Function Remeber that Blitz3D don't remember more than 6 numbers after comma. |