Languages & Coding > Blitz2D, BlitzPlus, Blitz3D

round values

(1/2) > >>

Santiago:
Hi, I have a question that must be silly.

how can I do to round values in a variable,

val # = 0.29382918
I want it to be val # = 0.29

I want to do a function, where I have the variables only with 2 decimal places.

Is there a simple way or command to do it?

my brain is refusing to create such a function.

Would you give me some tip where to start?
Cheers!

Santiago:
i want something like this

print "lat : " + round(variable_latitud#,2)

2 is the precision i want to see, but without modify my variable

GfK:
You can't.  Floating point numbers don't have that level of accuracy, and you'll (most of the time) end up with something like 0.2899999999.  And that goes for every language - not just the Blitz stuff.

If you do need 100% accuracy to two decimal places for display purposes, then the best way is to store the whole number and the decimal part as two separate integers.

Santiago:
Sure, but I only want to round the numbers so that they are easy to read, that they only have 2 decimal places for example.

i make this one, but dosent work like i want :)

--- Code: ---Function round#(variable#,decimales)

dec# = (variable# - Int(variable#))*10
dec# = Int(dec)

dec# = (Left(dec,decimales+1))
dec# = dec# *.1

variable# = Int(variable#) + Float(dec#)

Return variable#

End Function
--- End code ---

Santiago:
i add some round with a precition of .01, but i need  to work more to find a solution.

--- Code: --- If Abs(variable - Int(variable)) < .01 Then
variable# = Int(variable)
End If

--- End code ---