meters To Pretty Distance - Java java.lang

Java examples for java.lang:Math Convert

Description

meters To Pretty Distance

Demo Code


//package com.java2s;
import java.text.DecimalFormat;

public class Main {
    public static String metersToPrettyDistance(Double meters) {
        if (meters == null) {
            return "Unknown";
        }/* w  w  w.ja v  a 2 s .  c o m*/

        Double miles = metersToMiles(meters);

        if (miles == null) {
            return "Unknown";
        }

        // Display distances < 0.1 miles in feet
        if (miles < 0.1) {
            Double feet = metersToFeet(meters);

            if (feet == null) {
                return "Unknown";
            }

            return new DecimalFormat("#").format(feet) + " ft";
        } else {
            return new DecimalFormat("#.#").format(miles) + " mi";
        }
    }

    public static Double metersToMiles(Double meters) {
        try {
            return meters / 1609.344;
        } catch (Exception e) {
            return null;
        }
    }

    public static Double metersToFeet(Double meters) {
        try {
            return meters * 3.2808;
        } catch (Exception e) {
            return null;
        }
    }
}

Related Tutorials