Java tutorial
//package com.java2s; public class Main { public static String precisionDistance(double distance) { if (distance < 0.1) { return String.valueOf((int) (distance * 1000)); } if (distance >= 0.1 && distance < 1) { int temp = (int) (distance * 1000); if (temp % 10 < 5) return String.valueOf((temp / 10) * 10); else return String.valueOf(((temp + 10) / 10) * 10); } if (distance >= 1 && distance < 10) { return String.valueOf(roundDistance(distance)); } else { return String.valueOf((int) distance); } } public static double roundDistance(double val) { return (Math.floor(val * 10)) / 10; } }