Java tutorial
//package com.java2s; import java.text.DecimalFormat; public class Main { /** * Format distance to be displayed to the user * * @param d , distance in metres * * @return formatted {@link String} */ public static String formatDistance(double d) { DecimalFormat formatter = new DecimalFormat("#,### 'm'"); d = Math.round(d); if (d > 1000) { formatter = new DecimalFormat("#,###.# 'km'"); d = d / 1000; } return formatter.format(d); } }