Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Round to closest list value?
EvanReese
Member, Mentor Posts: 2,136 ✭✭✭✭✭
Does anyone know of a way that I can take a number and round it to the nearest value in a predetermined list (maybe an array)? For example:
if my list is [1,4,8,11,12]
1.9 rounds to 1
9.3 rounds to 8
9.5 rounds to 11
11.6 rounds to 12
There's got to be a name for such a thing.
if my list is [1,4,8,11,12]
1.9 rounds to 1
9.3 rounds to 8
9.5 rounds to 11
11.6 rounds to 12
There's got to be a name for such a thing.
Evan Reese
0
Comments
ceil() rounds up to nearest whole number
floor() rounds down to nearest whole number
round() rounds to nearest up or down to nearest whole number
roundToPrecision() will round with decimals
<pre class="CodeBlock"><code>function maxItem(container is array, estimationFunction is function)<br>{<br> var max = container[0];<br> for (var item in container)<br> {<br> if (estimationFunction(item) > estimationFunction(max))<br> {<br> max = item;<br> }<br> }<br> return max;<br>}