Find the number closest to n and divisible by m - rFronteddu/general_wiki GitHub Wiki

static int closestNumber(int n, int m)  {
        // find the quotient
        int q = n / m;
        
        // 1st possible closest number
        int n1 = m * q;
        
        // 2nd possible closest number
        int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));
        
        // return closer number
        return Math.abs(n-n1) < Math.abs(n-n2) ? n1 : n2;
    }