December 04, 2020, 02:19:05 PM

Author Topic: round values  (Read 288 times)

Offline Santiago

  • Sr. Member
  • ****
  • Posts: 283
  • Blitz3d
    • Indiesoft
round values
« 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!

Offline Santiago

  • Sr. Member
  • ****
  • Posts: 283
  • Blitz3d
    • Indiesoft
Re: round values
« Reply #1 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

Online GfK

  • Full Member
  • ***
  • Posts: 166
Re: round values
« Reply #2 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.
Intel I9-9900K 3.6-5.0GHz | GeForce RTX2070 8GB | 32GB RAM | 500GB NVMe M.2 SSD | 1TB HDD | Windows 10 x64.
MSI Apache Pro | I7-7700HQ | GeForce GTX1060 3GB | 8GB RAM | 128GB SSD | 1TB HDD | Windows 10 x64.

Offline Santiago

  • Sr. Member
  • ****
  • Posts: 283
  • Blitz3d
    • Indiesoft
Re: round values
« Reply #3 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

Offline Santiago

  • Sr. Member
  • ****
  • Posts: 283
  • Blitz3d
    • Indiesoft
Re: round values
« Reply #4 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

Offline Henri

  • Sr. Member
  • ****
  • Posts: 265
Re: round values
« Reply #5 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.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

Offline Ashmoor

  • Jr. Member
  • **
  • Posts: 85
Re: round values
« Reply #6 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.  

Offline Derron

  • Hero Member
  • *****
  • Posts: 3275
Re: round values
« Reply #7 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

Offline Ashmoor

  • Jr. Member
  • **
  • Posts: 85
Re: round values
« Reply #8 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

Offline RemiD

  • Hero Member
  • *****
  • Posts: 1081
Re: round values
« Reply #9 on: November 28, 2020, 09:21:30 PM »
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

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal