LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to round a number to a specific value and not to a number of decimals

Solved!
Go to solution

Hello,

I would like to round numbers following a grid.

Let's say I have a grid of 6 between 3 and 4 ( 3.0 3.2 3.4 3.6 3.8 4.0)

I would like to round to the nearest number of the grid, so if I choose 3.7125, it will be round to 3.8.

I found already in the forum how to round to  certain degree of decimal but not in my approach.

Do you know to force the rounding to the nearest of the grid.

I was thinking of a ramp up to make the grid then interpolate the number and use its index to round. But I am sure there is  a much easier way.

Thanks,

Zied

0 Kudos
Message 1 of 10
(5,648 Views)

I did the following but I am not sure if it can be easier as I feel it is ressources consuming as the speed does matter.

0 Kudos
Message 2 of 10
(5,641 Views)

@ziedhosni wrote:

Hello,

I would like to round numbers following a grid.

Let's say I have a grid of 6 between 3 and 4 ( 3.0 3.2 3.4 3.6 3.8 4.0)

I would like to round to the nearest number of the grid, so if I choose 3.7125, it will be round to 3.8.

I found already in the forum how to round to  certain degree of decimal but not in my approach.

Do you know to force the rounding to the nearest of the grid.

I was thinking of a ramp up to make the grid then interpolate the number and use its index to round. But I am sure there is  a much easier way.

Thanks,

Zied


Not exactly sure what you mean, but does "Interpolate 1 D Array" do it for you?

Bill
CLD
(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 3 of 10
(5,638 Views)

Oops I see what you are trying to do.  I like your approach, but let's see what the heavy hitters here think of it.  They often have better solutions because they are the gurus.

Bill
CLD
(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 4 of 10
(5,636 Views)

Here's a possibility.

 

nrearest value.png

 

Ben64

Message 5 of 10
(5,629 Views)

Here's a version that doesn't use arrays, so it theoretically scales to infinity without an increase in memory space or processing time.

Round to grid.png

(The "False" case just wires the coerce output straight through)

 However it doesn't work with grids that don't have even steps between the numbers, and I don't know if that's a possibility.

Message 6 of 10
(5,620 Views)
Solution
Accepted by topic author ziedhosni

Those ideas all seem to work if you are trying to round to a set of specific values.  But you need to define the specifc values for all possibilities.

 

If you want to round to a specific interval, there is an easier way to do it.

So you want to round to each 0.2.  Divide your number by that interval.  Round to the nearest integer.  Multiply your number by that interval. 

 

 

 

If you need to force a number that is lower or higher than a given range to be within that range, such as having 1.255 round UP to 3, then put an In Range and Coerce either before or after this bit of code.

Message 7 of 10
(5,617 Views)

@ziedhosni wrote:

Hello,

I would like to round numbers following a grid.

Let's say I have a grid of 6 between 3 and 4 ( 3.0 3.2 3.4 3.6 3.8 4.0)

I would like to round to the nearest number of the grid, so if I choose 3.7125, it will be round to 3.8.

 

 


Zied,

     Usually when one talks about "rounding", they aren't talking about numbers restricted to a range, say between 3 and 4.  As an example, what would you want 1.55 to round to, 3.0 or 1.6?

     If you want to "round to the nearest 0.2" (and, perhaps, later deal with the question of restricting numbers between 3 and 4), you can "play mathematician" and solve your problem: divide by the interval 0.2 (making them range from 15 to 20), round to the nearest integer, then multiply by the interval (bringing them back to the range 3 .. 4).  As long as you have an evenly spaced grid that lands on an integer, you can use this trick.

 

Bob Schor

Message 8 of 10
(5,607 Views)

Thank you guys. Unfortunately, I cannot choose all the solutions . I picked up one.

I hope I can get as many feedback for another post I put 2 weeks ago and it is not solved yet .

Heart

Cheers,

Zied

0 Kudos
Message 9 of 10
(5,585 Views)

This is what the Threshold 1D Array function does - as long as your array of numbers is in ascending order.

0 Kudos
Message 10 of 10
(5,517 Views)