Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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?

Evan_ReeseEvan_Reese Member Posts: 239 PRO
edited May 16 in Using Onshape
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.
Evan Reese / Agency Owner and Industrial Designer
Website: fractalmade.com
Instagram: @evan.reese.designs

Comments

  • john_mcclaryjohn_mcclary Member, Developers Posts: 2,709 PRO
    There are a few rounding funcitons

    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

    var listStart = [1.9859,9.23,9.2525,11.652,0.654110111];     
            var listFloor = [];
            var listCeil = [];
            var listRound = [];
            var listRoundToPrecision = [];
            
            for (var item in listStart)
            {
                listFloor = append(listFloor, floor(item));
                listCeil = append(listCeil, ceil(item));
                listRound = append(listRound, round(item));
                listRoundToPrecision = append(listRoundToPrecision, roundToPrecision(item, 2));
            }
            
            println("Original List: " ~ listStart);
            println("Rounded List Down: " ~ listFloor);
            println("Rounded List Up: " ~ listCeil);
            println("Rounded List Nearest: " ~ listRound);
            println("Rounded List Nearest (2 place decimal): " ~ listRoundToPrecision);
    
    
    // result \\
    Original List: [ 1.9859 , 9.23 , 9.2525 , 11.652 , 0.654110111 ]
    Rounded List Down: [ 1 , 9 , 9 , 11 , 0 ]
    Rounded List Up: [ 2 , 10 , 10 , 12 , 1 ]
    Rounded List Nearest: [ 2 , 9 , 9 , 12 , 1 ]
    Rounded List Nearest (2 place decimal): [ 1.99 , 9.23 , 9.25 , 11.65 , 0.65 ]
    
    

  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 993 ✭✭✭✭✭
    edited May 16
    I have a function maxItem() which returns a max value of the list based on some other estimation function:
    function maxItem(container is array, estimationFunction is function)
    {
        var max = container[0];
        for (var item in container)
        {
            if (estimationFunction(item) > estimationFunction(max))
            {
                max = item;
            }
        }
        return max;
    }
    say we have a value "value" and estimation function may be:
    const estFunction = function(x){return -abs(x - value)};
    then you just do this:
    const roundedItem = maxItem(roundedArr, estFunction);
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 977
    If your array is small, Konstantin's method works fine.  If you have a large array, the efficient method for solving this problem is binary search: https://en.wikipedia.org/wiki/Binary_search_algorithm#Procedure_for_finding_the_leftmost_element (use that procedure and then pick the closer one of A[L] and A[L-1])
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • Evan_ReeseEvan_Reese Member Posts: 239 PRO
    Thanks for all the info everyone!
    Evan Reese / Agency Owner and Industrial Designer
    Website: fractalmade.com
    Instagram: @evan.reese.designs
Sign In or Register to comment.