Converts the length value from kilometers to miles. - Java java.lang

Java examples for java.lang:Math Convert

Description

Converts the length value from kilometers to miles.

Demo Code


//package com.java2s;

public class Main {
    /**/*from   www  .ja va  2s .  c  o  m*/
     * Converts the length value from kilometers to miles. The round is very
     * usefull for displaying values in english unit mode. This avoids problems
     * when e.g. the user has entered 45 miles, he will see e.g. 44,999999 miles
     * again. The reason is the internal storage in metric unit system.
     *
     * @param kilometers value in kilometers
     * @param fRound flag for round the result with 3 decimals
     * @return value in miles
     */
    public static double convertKilometer2Miles(double kilometers,
            boolean fRound) {
        double dResult = kilometers / 1.609344d;
        if (fRound) {
            long lValue = Math.round(1000 * dResult);
            return lValue / 1000d;
        } else {
            return dResult;
        }
    }

    /**
     * Converts the length value from kilometers to miles.
     *
     * @param kilometers value in kilometers
     * @return value in miles
     */
    public static int convertKilometer2Miles(int kilometers) {
        return (int) Math.round(convertKilometer2Miles((double) kilometers,
                false));
    }
}

Related Tutorials