Java Double Number Round round(final double val, final int decimalPlaces)

Here you can find the source of round(final double val, final int decimalPlaces)

Description

Rounds a coordinate value (longitude or latitude) to a specific precision.

License

Apache License

Parameter

Parameter Description
val The coordinate value.
decimalPlaces The number of decimal place to round to.

Return

The rounded value.

Declaration

public static double round(final double val, final int decimalPlaces) 

Method Source Code

//package com.java2s;
/**//  w  w  w  .j  a va2s .com
 * Licensed to the TomTom International B.V. under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  TomTom International B.V.
 * licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

public class Main {
    /** The default precision for rounding coordinate values. */
    private static final int DEFAULT_PRECISION = 5;
    /** Integer of value 10. */
    private static final int TEN = 10;

    /**
     * Round.
     * 
     * @param val
     *            the val
     * @return the double
     */
    public static double round(final double val) {
        return round(val, DEFAULT_PRECISION);
    }

    /**
     * Rounds a coordinate value (longitude or latitude) to a specific
     * precision.
     * 
     * @param val
     *            The coordinate value.
     * @param decimalPlaces
     *            The number of decimal place to round to.
     * @return The rounded value.
     */
    public static double round(final double val, final int decimalPlaces) {
        return Math.round(val * Math.pow(TEN, decimalPlaces)) / Math.pow(TEN, decimalPlaces);
    }
}

Related

  1. round(double[][] data)
  2. round(final double num)
  3. round(final double num, final int prec)
  4. round(final double numberToRound, final int decimalPlaces)
  5. round(final double v)
  6. round(final double val, final int decimals)
  7. round(final Double value)
  8. round(final Double value)
  9. round(final double value, final int numDecimals)