package fr.pandacube.lib.util; import java.text.DecimalFormat; import java.util.Arrays; /** * This class contains various methods to manipulate and display distances. */ public class DistanceUtil { /** * Generate a string representation of the provided distance, with a specific decimal precision and one of the * specified metric prefix * @param meterDist the distance to display, in meter * @param precision the number of digit to display after the decimal separator * @param desiredUnits the preferred unit prefix to use for conversion. * @return a string representation of the provided distance */ public static String distanceToString(double meterDist, int precision, DistanceUnit... desiredUnits) { Arrays.sort(desiredUnits); DistanceUnit chosenUnit = desiredUnits[0]; // la plus petite unité for (DistanceUnit unit : desiredUnits) { if (meterDist / unit.multiplier < 1) continue; chosenUnit = unit; } if (chosenUnit != desiredUnits[0] && precision <= 2) precision = 2; String precisionFormat = "##0"; if (precision > 0) precisionFormat += "."; precisionFormat += "0".repeat(precision); DecimalFormat df = new DecimalFormat(precisionFormat); double dist = meterDist / chosenUnit.multiplier; return df.format(dist) + chosenUnit.unitStr; } /** * * Generate a string representation of the provided distance, with a specific decimal precision, and using either * {@code km} or {@code m} as a unit. *
* Calling this method is equivalent to {@code distanceToString(meterDist, precision, DistanceUnit.M, DistanceUnit.KM)}. * @param meterDist the distance to display, in meter * @param precision the number of digit to display after the decimal separator * @return a string representation of the provided distance */ public static String distanceToString(double meterDist, int precision) { return distanceToString(meterDist, precision, DistanceUnit.M, DistanceUnit.KM); } /** * Enumeration of commonly used distance metric unit */ public enum DistanceUnit { /** * Nanometer unit. One billionth of a meter. 10-9 = 0.000000001m. */ NM(0.000000001, "nm"), /** * Micrometer unit. One millionth of a meter. 10-6 = 0.000001m. */ UM(0.000001, "µm"), /** * Millimeter unit. One thousandth of a meter. 10-3 = 0.001m. */ MM(0.001, "mm"), /** * Centimeter unit. One hundredth of a meter. 10-2 = 0.01m */ CM(0.01, "cm"), /** * Meter unit. One meter. 100 = 1m. */ M(1, "m"), /** * Kilometer unit. One thousand meter. 103 = 1000m. */ KM(1000, "km"); /** * The value of this unit in meter. */ public final double multiplier; /** * String representation of the unit symbol. */ public final String unitStr; DistanceUnit(double multiplier, String s) { this.multiplier = multiplier; unitStr = s; } } }