# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## rounding off to nearest decimal point

i have these values

 35.2 35.3 35.4 35.5 35.7 35.8 35.9 36.0

i am looking for indexes closet 35.74 in my array. (which is index 4.)

i used the "round to nearest" function and it gave me 36.

which is index 7.

what other ways to get the right index?

Message 1 of 18
(7,788 Views)

## Re: rounding off to nearest decimal point

?

I guess you can just take the absolute value of the difference and the smallest one is the closest one?

I actually have no idea what you are trying to do.

Bill

(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 2 of 18
(7,782 Views)

## Re: rounding off to nearest decimal point

i want to find

the index closest to

value 35.74

just by looking at it. it should be 35.7 or 35.8

i tried using the round to nearest function and it rounded out to be 36.00

how can i make it so that i can get 35.7?

Message 3 of 18
(7,752 Views)
Solution
Accepted by topic author super_saiyans

## Re: rounding off to nearest decimal point

It should be noted that you'll need a sorted array as in your example.  If that's just a simple explanation for a larger context, you may need something more sophisticated.

--
Tim Elsey
Certified LabVIEW Architect
Message 4 of 18
(7,736 Views)

## Re: rounding off to nearest decimal point

You will always have some difficulties with rounding to a specific number of digits after the decimal point because of the way numbers are represented in binary. When expressed in binary 0.1 is an infinitely repeating expression. Regardless of the display the internal representation of the number always uses the maximum resolution for the data type (8 bytes for DBL).

So you need to be very careful to define exactly what you  want. If you have an array of values (regardless of the way they are displayed) Then you can find the closest to a Test Value by subtracting Test Value from the array and finding the minimum of the absolute values of the differences.

Note that in the image below the same values are in Array and Array 2 with the display on Array 2 set for 20 siginficant digits.

Lynn

Message 5 of 18
(7,729 Views)

## Re: rounding off to nearest decimal point

thanks!!

both method works!!

thank you, but my data is not sorted so it wont work. but the example worked.

thank you!

Message 6 of 18
(7,713 Views)

## Re: rounding off to nearest decimal point

Really I should get an honorable mention (i.e., kudo) for being the first one to mention the method you chose as the solution.  😉

Bill

(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 7 of 18
(7,686 Views)

lol.

THANK YOU BOTH

Message 8 of 18
(7,639 Views)

## Re: rounding off to nearest decimal point

See Here Also

Seen Here

Obviously, X should be a Float but I was too lazy to fix it

"Should be" isn't "Is" -Jay
Message 9 of 18
(7,627 Views)

## Re: rounding off to nearest decimal point

One other comment about rounding.  LabVIEW uses banker's rounding which takes any value that is dead on 0.5 and will round it to the nearest even integer.  Generally in school we would have learned that 0.5 would be rounded up.

So nearest even integer may or may not be what you are looking for.

Message 10 of 18
(7,620 Views)