hello i have a code in vb.net and my logic is if i will input 13973.88 the nearest number is 10000 but the output was 15833 can you please help me solving this code please?

Dim data(,) = {
{0.0, 0, 1},
{0.0, 0.05, 4167},
{41.67, 0.1, 5000},
{208.33, 0.15, 6667},
{708.33, 0.2, 10000},
{1875.0, 0.25, 15833},
{4166.67, 0.3, 25000},
{10416.67, 0.32, 45833}}

Dim value = Val(TextBox18.Text)

' data.GetLength(0) is 8, so Enumerable.Range generates the numbers from 0 to 7
Dim range = Enumerable.Range(0, data.GetLength(0))

' .OrderBy orders them by the difference between value and the last column in data
Dim ordered = range.OrderBy(Function(i) Math.Abs(data(i, 2) - value)) ' 5,4,6,3,2,0,1,7

Dim nearestIndex = ordered.First ' 5

Dim value1 = data(nearestIndex, 0) ' 1875.0
Dim value2 = data(nearestIndex, 1) ' 0.25
Dim value3 = data(nearestIndex, 2) ' 11667

Dim result = (value3) ' result = 1541.75

TextBox15.Text = Math.Round(result, 2)
End If

## All 2 Replies

13973.88 is closer to 15833 than it is to 10000 so what is the problem?

Not sure if we should be answering, since you didn't acknowledge any of the help you got on your other question about the same problem. Opening new questions about the same problem is not really going go get you anywhere. Also the code you're showing isn't even trying to solve the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.