Is there a simpler way??

jschuchert

Active Member
Licensed User
Longtime User
I tend to make things more complicated than they really are but would like someone to look at the following code and tell me if there would have been a better and easier way. The code works. Here's the problem:

If the decimal after a value is = or greater than .5 then I want to use "floor" with the next statement or "round" if it is less. The result will be the same number but that's what I want it to be.

Code: Copy
   Dim part1,part2
   Dim brgdms As String 'so main.bearingdms can be converted to string
   If Main.dblBearingdms >= 0 Then
   brgdms=Main.dblbearingdms  'convert to a string
   brgdms=brgdms.Replace(".",",") ' replace the decimal in the number with a comma so it can be parsed
   If brgdms.IndexOf (",")>0 Then  'verify the value has a decimal. Assume it has
   parts=Regex.Split(",",brgdms)  'parse it
   part1=parts(0) 'value in front of decimal
   part2=parts(1)  'value behind decimal
   End If
   x= part2.IndexOf("5")  'find the index of the number 5 if present..it must be the first digit after the decimal to fit the requirement for flooring
    If x=0 Then
   deg=Floor(Main.dblbearingdms)
   Else
   deg=Round(Main.dblbearingdms)
   End If
   End If

Thanks,
Jim S.
 
Perhaps something like
If yournumber*10 mod 10 >=5 then newnumber=floor(yournumber) else newnumber=round(yournumber)
 
Upvote 0
I don't understand exactly what you want to but B4A has two functions:
Floor and Ceil.
Floor(4.6) = 4
Ceil(4.6) = 5
Floor(-4.6) = -5
Ceil(-4.6) = -4

I think you could work with these two functions directly on the number to achieve what you want.

Best regards.
 
Upvote 0
I don't understand exactly what you want to but B4A has two functions:
Floor and Ceil.
Floor(4.6) = 4
Ceil(4.6) = 5
Floor(-4.6) = -5
Ceil(-4.6) = -4

I think you could work with these two functions directly on the number to achieve what you want.

Best regards.

Klaus is right. Anyway, flooring a decimal>=0.5 will return its int part, while rounding a decimal<0.5 will return the same, thus floor() will give the correct answer in both cases.
 
Upvote 0
From your description and your code I think this is what you want:

Code: Copy
If Main.dblBearingdms >= 0 Then
deg=Floor(Main.dblbearingdms)
End If

Are you sure that code works the way you want? If the decimal part is .6 or greater it'll round up.
 
Last edited:
Upvote 0
Thanks to all for your input. Further checking on my part reveals my explanation of the problem was not clear but your responses have made me re-think the issue, during which I have uncovered other items that must be handled. There are several variables (not the programming kind) not addressed in the code but hopefully I can resolve them. If not, I will post back. Thanks again.

Jim S.
 
Upvote 0
Back
Top