# question

## Checking if a decimal number is between the top half or the bottom half or the same in VB.NET.

I'm trying to check if a decimal number is in the bottom half or the top half with VB.NET.

For example:

If the number is 2.3 or 1.25333 or 1.000001 or 1, then it reports that the number is in the bottom half.
If the number is 0.5, 1.5, 2.5, 3.5, 4.5, then it reports that the number is in the middle
If the number is 0.7, 0.50001, 3.6111. 104.99999, then it reports that the number is in the top half.

This example means that if the numbers in the tenth place is greater then 5, then it reports that the number is in the top half. If the number is less then 5, then it reports the number is in the bottom half and if the number is equal to 5, then it reports that the number is equal to 5.

I cannot make this directly by testing for the number in the tenth place directly because the other numbers after it would need to be checked or rounded (for example, if the number is 3.5000000001, then it is not directly 5 but there is still a 5 in the tenth place).

How can I make a function that tells me if the number is in the top half, middle, or the bottom half in VB.NET?

· 3

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you have negative numbers too?

@Viorel-1 Yes

To avoid confusions, show the rules for negative numbers.

1 Vote"

Hi,
try following code:

``````   Dim numbers As New List(Of Decimal)(New Decimal() {2.3D, 1.25333D, 1.000001D, 1, 0.5D, 1.5D, 2.5D, 3.5D, 4.5D, 0.7D, 0.50001D, 3.6111D, 104.99999D})
For Each n0 In numbers
Dim n1 = n0 - Math.Floor(n0)
Dim result = If(n1 < 0.5, "Buttom", If(n1 > 0.5, "Top", "Middle"))
Console.WriteLine(\$"{n0} - {result}")
Next
``````

Result:

2,3 - Buttom
1,25333 - Buttom
1,000001 - Buttom
1 - Buttom
0,5 - Middle
1,5 - Middle
2,5 - Middle
3,5 - Middle
4,5 - Middle
0,7 - Top
0,50001 - Top
3,6111 - Top
104,99999 - Top

·

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Vote"

Hi,
for negative values you can use following demo:

``````   Dim numbers As New List(Of Decimal)(New Decimal() {2.3D, -1.25333D, 1.000001D, 1, 0.5D, -1.5D, 2.5D, 3.5D, 4.5D, 0.7D, 0.50001D, -3.6111D, 104.99999D})
For Each n0 In numbers
Dim n1 = Math.Abs(n0) - Math.Floor(Math.Abs(n0))
Dim result = If(n1 < 0.5, "Buttom", If(n1 > 0.5, "Top", "Middle"))
Console.WriteLine(\$"{n0} - {result}")
Next
``````

Result:

2,3 - Buttom
-1,25333 - Buttom
1,000001 - Buttom
1 - Buttom
0,5 - Middle
-1,5 - Middle
2,5 - Middle
3,5 - Middle
4,5 - Middle
0,7 - Top
0,50001 - Top
-3,6111 - Top
104,99999 - Top

·

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.