round values

Started by Santiago, October 14, 2020, 14:39:38

Previous topic - Next topic

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

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 :)


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


Santiago

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

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

Henri

#4
Hi,

in order to show a floating point value in two decimal format you need to use strings.

Something like (in Bmax but should be easily adapted)..


'Example output prints 2.12

Local d:Double = 2.1234567

Print RoundDouble(d)

Function RoundDouble:String(d:Double, precision:Int=2)
Local s:String

Select precision
Case 0 Return String( Int(d + 0.5) )
Case 1 s = String(d + 0.05)
Case 2 s = String(d + 0.005)
Default s = String(d + 0.0005)
EndSelect

Local a:String[], decimal:String

If s.contains(".") Then
a = s.split("."); decimal = "."
ElseIf s.contains(",")
a = s.split(","); decimal = ","
Else
Notify "Error: RoundString() failed with " + s + ".", True; Return s
EndIf

Return (a[0] + decimal + Left( a[1], precision) )
EndFunction


Just to note that this is not the only way, but every way uses strings (or integers as Gfk pointed out).

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Ashmoor

#5
Here is my blitzmax function for drawing two decimal floats, it's not exactly what you are looking for but I think you can use it to split your float in two ints and do the rounding:

Code (blitzmax) Select

Local x:Float = 33.2651

Print (GetTwoDecimalFloat(x))


Function GetTwoDecimalFloat:String(nbr:Float)
Local nbrStr:String
Local posCut:Int

nbrStr = String(nbr)
posCut = nbrStr.Find(".")
nbrStr = Left(nbrStr, posCut + 3)

Return nbrStr

End Function

Derron

Use Henri's over Ashmoor's variant. Ashmoor's will fail on eg "Linux distribution with German localization" (we write "1,23" instead of "1.23" - and "stringifying" a number leads to exactly this comma then). You can only disable it by setting the "to use" locale on start of your application.


bye
Ron

Ashmoor

@Derron, and that is how one finds out about problems they never knew they had :D Thanks