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

blinkor12 101 Reputation points
2021-02-27T04:32:17.407+00:00

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?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,575 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-02-27T07:25:43.17+00:00

    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

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-02-27T21:18:44.2+00:00

    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

    1 person found this answer helpful.
    0 comments No comments