Utility method to format the distance in meters - Android java.lang

Android examples for java.lang:Math

Description

Utility method to format the distance in meters

Demo Code


//package com.java2s;
import android.content.Context;

public class Main {
    /**//from w  ww  .  ja v  a2s  .  c  om
     * Number fo meters in a Km
     */
    private static final int METERS_IN_A_KM = 1000;

    /**
     * Utility method to format the distance in meters
     *
     * @param context  The Context
     * @param distance The distance in meters
     * @return The distance as a String
     */
    public static String formatDistance(final Context context,
            final float distance) {
        if (distance < METERS_IN_A_KM) {
            return String.format("%.0f m", distance);
        } else {
            return String.format("%.2f km", (distance / METERS_IN_A_KM));
        }
    }
}

Related Tutorials