# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

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

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

Message 1 of 10
(3,056 Views)

## Re: How to round a number to a specific value and not to a number of decimals

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.

Message 2 of 10
(3,049 Views)

## Re: How to round a number to a specific value and not to a number of decimals

@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

(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
(3,046 Views)

## Re: How to round a number to a specific value and not to a number of decimals

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

(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
(3,044 Views)

## Re: How to round a number to a specific value and not to a number of decimals

Here's a possibility.

Ben64

--------------------------------------------------
The best way to say thanks is to give kudos!
Message 5 of 10
(3,037 Views)

## Re: How to round a number to a specific value and not to a number of decimals

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

(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
(3,028 Views)
Solution
Accepted by ziedhosni

## Re: How to round a number to a specific value and not to a number of decimals

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
(3,025 Views)

## Re: How to round a number to a specific value and not to a number of decimals

@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
(3,015 Views)

## Re: How to round a number to a specific value and not to a number of decimals

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 .

Cheers,

Zied

Message 9 of 10
(2,993 Views)

## Re: How to round a number to a specific value and not to a number of decimals

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

Message 10 of 10
(2,925 Views)