JMU
Rounding
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
Review
Thinking About the Problem
The Pattern
javaexamples/programmingpatterns/Rounding.java (Fragment: pattern)
            /**
     * Round an int value to a given place (i.e., power of 10).
     *
     * @param number  The number to round
     * @param place   The place to round to (10, 100, 1000, etc...)
     * @return The rounded value
     */
    public static int round(int number, int place) {
        int rounded, truncated;
        
        truncated = (number / place) * place;

        if ((number % place) >= (place / 2)) {
            rounded = truncated + place;
        } else {
            rounded = truncated;
        }

        return rounded;
    }
        
Examples
Examples (cont.)