LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Rounding Values

Hi Everyone,
 
A quick question, probably an easy solution!
 
My system will generate numbers which I need to round up to the nearest 500. An example of what I would like to happen is show below:
 
Number = 1300 gets rounded upto 1500
Number = 1950 gets rounded upto 2000
Number = 1100 gets rounded down 1000
 
The values are not always in single thousands e.g. Number = 12,300 gets rounded upto 12,500 and similarly Number = 400 gets rounded upto 500.
 
Any easy solution to this?
 
 
0 Kudos
Message 1 of 14
(4,542 Views)
Use "Quotient and Reminder", Add, Multiply and a Select. Or my solution in LV 8.2.
Waldemar

Using 7.1.1, 8.5.1, 8.6.1, 2009 on XP and RT
Don't forget to give Kudos to good answers and/or questions
0 Kudos
Message 2 of 14
(4,527 Views)
Hi intel,

what about this formula: rounded = 500* round_to_nearest(x/500) (You better use floating point numbers for this 🙂
You can make a sub-vi from it... Change the 500 from constant to control and you have function to round to arbitrary numbers!
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 3 of 14
(4,519 Views)
Hi Waldemar,
 
I thought of something similar to that Smiley Happy thanks for the solution. Only problem with it is if the value is say 10, then the final number should be 0. It needs to add the rounding down to it as well, so just gotta get my head around that.
 
Thanks for your help.

p.s. I will give the second solution a try too.

Message Edited by intel on 03-08-2007 10:52 AM

0 Kudos
Message 4 of 14
(4,520 Views)
Sorry from your text My system will generate numbers which I need to round up to the nearest 500 I assumed just rounding up. I have seen the third example will round down. Change my solution in the following way:

Replace the comparison function "Equal to 0?" with the function "In Range and Coerce". Wire 0 and the highest number you want to round down. Check "Include upper limit". In the case "In Range" add 0 otherwise add 1.

And I don't know if it will work with negative numbers.

Message Edited by waldemar.hersacher on 03-08-2007 08:45 PM

Waldemar

Using 7.1.1, 8.5.1, 8.6.1, 2009 on XP and RT
Don't forget to give Kudos to good answers and/or questions
0 Kudos
Message 5 of 14
(4,498 Views)
GerdW's solution is the most elegant way to solve it (and especially looks pretty when placed in an Expression Node).

Just to mention: The behaviour of "Round To Nearest" will result in (when rounding to 500):
250 is rounded to 0.
750 is rounded to 1000.
1250 is rounded to 1000.     ... (x.5 numbes are rounded to the next even number).

I'm sure you considered this,  Guenter
0 Kudos
Message 6 of 14
(4,490 Views)
Here's what I believe is the standard method for handling rounding. It works for negative numbers and doesn't have an if statement in it (so it's likely faster, if that even matters).

To round down in all cases, remove the first division (quotient) and addition
To round up just wire the round to nearest directly into the addtion and remove the first division.
0 Kudos
Message 7 of 14
(4,483 Views)
Thanks all,
 
It is not necessary for the solution to handle negative numbers, if there are negative numbers being received then there are major problems!
 
Once again, thanks this should make my life a lot easier and helps me with some other stuff I am working on too.
0 Kudos
Message 8 of 14
(4,471 Views)
Dear Matt,

taking another divide into account your solution is 2 nodes faster than mine.

The results of our solution is different from the results of the solution using "Round to nearest", which isn't available in a expression node. 250 will give 500 in our solution but will give 0 with "Round to nearest".
Waldemar

Using 7.1.1, 8.5.1, 8.6.1, 2009 on XP and RT
Don't forget to give Kudos to good answers and/or questions
0 Kudos
Message 9 of 14
(4,469 Views)
I cant seem to open your solution Matt. I am only running LabVIEW 8.0 maybe this is the problem.
 
Still trying your solution out Waldemar Smiley Happy
0 Kudos
Message 10 of 14
(4,461 Views)