# SyntaxBomb - Indie Coders

## Languages & Coding => Blitz2D, BlitzPlus, Blitz3D => Topic started by: Santiago on October 14, 2020, 02:39:38 PM

Title: round values
Post by: Santiago on October 14, 2020, 02:39:38 PM
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!
Title: Re: round values
Post by: Santiago on October 14, 2020, 02:40:48 PM
i want something like this

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

2 is the precision i want to see, but without modify my variable
Title: Re: round values
Post by: GfK on October 14, 2020, 02:43:13 PM
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.
Title: Re: round values
Post by: Santiago on October 14, 2020, 02:51:15 PM
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: [Select]
`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`
Title: Re: round values
Post by: Santiago on October 14, 2020, 03:00:25 PM
i add some round with a precition of .01, but i need  to work more to find a solution.

Code: [Select]
` If Abs(variable - Int(variable)) < .01 Then variable# = Int(variable) End If `
Title: Re: round values
Post by: Henri on October 14, 2020, 04:02:13 PM
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)..

Code: [Select]
`'Example output prints 2.12Local d:Double = 2.1234567Print 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
Title: Re: round values
Post by: Ashmoor on October 14, 2020, 06:48:51 PM
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
1. Local x:Float = 33.2651
2.
3. Print (GetTwoDecimalFloat(x))
4.
5.
6. Function GetTwoDecimalFloat:String(nbr:Float)
7.         Local nbrStr:String
8.         Local posCut:Int
9.
10.         nbrStr = String(nbr)
11.         posCut = nbrStr.Find(".")
12.         nbrStr = Left(nbrStr, posCut + 3)
13.
14.         Return nbrStr
15.
16. End Function
17.
Title: Re: round values
Post by: Derron on October 14, 2020, 09:58:35 PM
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
Title: Re: round values
Post by: Ashmoor on October 15, 2020, 06:02:11 PM
@Derron, and that is how one finds out about problems they never knew they had :D Thanks
Title: Re: round values
Post by: RemiD on November 28, 2020, 09:21:30 PM
https://www.syntaxbomb.com/index.php/topic,6393.0.html