Round up or down to specific number?

Hello,
How can I round down or up my values to specific numbers in code block?
Below screenshot with some random values:
Regards
obraz

Think you want this: Find closest value from list B into list A - #3 by Vikram_Subbaiah

2 Likes

Hello,
Here is this track dig

cordially
christian.stan

Unfortunately no.
Columns “Down” and “Up” should be in separate lists.
List “value” compare numbers from code block and generate two lists.
List A values rounded down and list B values rounded up.

@AeM_86 ,

i don`t if this is to straight forward … :wink:

KR

Andreas

1 Like

@Draxl_Andreas
That’s not the point.
For Your values list “Down” and “Up” should be:
obraz
Answers for list B and C are from list D.

if you’re up for some python, you could modify the approach mentioned in this SO post.

for example:

import bisect

def round_down(x):
    intvals = [5000, 7500, 10000, 20000, 30000, 40000, 50000]
    i = bisect.bisect_right(intvals,x)
    return intvals[i-1]

where “intervals” are your given list of numbers to round to and “i” returns the index of the position your number would fit in between. the function then returns the value BEFORE the place your number fits (“rounding down”).

of course you can adjust it to return both up and down, or handle cases where you cannot round down because it’s smaller than the given list.

2 Likes

The first link can be modified to suit your situation.

  1. Get the absolute distance from the sample to the up and down value.
  2. Find the index of the smallest distance value.
  3. Take the up/down value at that index.

As you only have a Up and Down value you could also use an if statement. In design script it would be something like this:
Math.Abs(sample-down) < Math.Abs(sample-up) ? down : up;

2 Likes

Maybe less professional but worked for me something like this for now.
But I’ll test this solution.

1 Like

hello, the es2 function is what you are looking for, I left the essai function just in case

def essai(numb:var[],lst:var[])
{
c=[];
loop=[Imperative]
{
for (i in (0..(DSCore.List.Count(numb)-1)))
	{
	if (numb[i] == lst[0])
		{
		c[i]=[lst[0],lst[1]];
		}
	else
		{
		d=DSCore.List.AddItemToEnd(numb[i],lst);
		e=DSCore.List.Sort(d);
		f=DSCore.List.IndexOf(e,numb[i]);
		c[i]=DSCore.List.GetItemAtIndex(e,[f-1,f+1]);
		}
	}
	return c;
}
return loop;
};
def es2(numb:var[],lst:var[])
{
c=[];
loop=[Imperative]
{
for (i in (0..(DSCore.List.Count(numb)-1)))
	{
	if (numb[i] == lst[0])
		{
		c[i]=[lst[0],lst[1]];
		}
	elseif (numb[i] == lst[(DSCore.List.Count(lst)-1)])
		{
		c[i]=[lst[(DSCore.List.Count(lst)-2)],
		lst[(DSCore.List.Count(lst)-1)]];
		}
	else
		{
		d=DSCore.List.AddItemToEnd(numb[i],lst);
		e=DSCore.List.Sort(d);
		f=DSCore.List.AllIndicesOf(e,numb[i]);
		g=DSCore.List.LastItem(f);
		c[i]=DSCore.List.GetItemAtIndex(e,[g-1,g+1]);
		}
	}
	return c;
}
return loop;
};

cordially
christian.stan

2 Likes

Hi,

I see there is a lot of solutions but just wanted to see if this approach could help .
If we simply put the ranges in and than determine what range does a number belong to it should solve it all.

inputs_1 = IN[0]
set_ranges =IN[1]
round_down = IN[2]

diff = []

for inp in inputs_1:
	range_index = 0
	while range < len(set_ranges):
		range_1 = set_ranges[range_index]
		range_2 = set_ranges[range_index+1]
		if range_1 < inp and inp < range_2:
			absolute_1 = abs(range_1 - inp) #Value needed if you want it to round down or up with an if statement
			absolute_2 = abs(range_2 - inp)
			if round_down:
				diff.append(range_1)
			else:
				diff.append(range_2)
			break
		range_index+=2
# Assign your output to the OUT variable.
OUT = diff


Hope this helps.

1 Like

Think I might have misunderstood at one point, but this is certainly much easier than infinitely nesting if statements and removes the need for both imperative code and python.

2 Likes

Cool, many thanks for help.
We can close topic.
Regards :slight_smile:

1 Like

Mark something as a solution for others please. :slight_smile:

2 Likes

Hello, the if was to manage the case where the value is positioned in index 0 after the list sort (negative index to be discarded)

cordially
christian.stan

1 Like