July 25, 2021, 17:41:41

### Author Topic: round values  (Read 465 times)

#### Santiago

• Sr. Member
• Posts: 290
• Blitz3d
##### round values
« on: October 14, 2020, 14:39:38 »
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

• Sr. Member
• Posts: 290
• Blitz3d
##### Re: round values
« Reply #1 on: October 14, 2020, 14:40:48 »
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

• Sr. Member
• Posts: 290
• Blitz3d
##### Re: round values
« Reply #2 on: October 14, 2020, 14:51:15 »
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`

#### Santiago

• Sr. Member
• Posts: 290
• Blitz3d
##### Re: round values
« Reply #3 on: October 14, 2020, 15:00:25 »
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 `

#### Henri

• Sr. Member
• Posts: 340
##### Re: round values
« Reply #4 on: October 14, 2020, 16:02:13 »
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
« Last Edit: October 14, 2020, 16:09:11 by Henri »
- Got 01100011 problems, but the bit ain't 00000001

#### Ashmoor

• Jr. Member
• Posts: 91
##### Re: round values
« Reply #5 on: October 14, 2020, 18:48:51 »
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.
« Last Edit: October 14, 2020, 18:52:50 by Ashmoor »

#### Derron

• Hero Member
• Posts: 3613
##### Re: round values
« Reply #6 on: October 14, 2020, 21:58:35 »
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

• Jr. Member
• Posts: 91
##### Re: round values
« Reply #7 on: October 15, 2020, 18:02:11 »
@Derron, and that is how one finds out about problems they never knew they had Thanks

#### RemiD

• Hero Member
• Posts: 1273
##### Re: round values
« Reply #8 on: November 28, 2020, 21:21:30 »
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0