public static string FormatDistance(double distance, UnitOfMeasure.EUnitType currentUnit, bool showCentimeter) { if (distance <= 0) { return(DbMessages.Geo_FormatDistance_ZeroMeter); } double distanceMetric; if (currentUnit == UnitOfMeasure.EUnitType.Kilometer) { distanceMetric = distance * 1000; } else if (currentUnit == UnitOfMeasure.EUnitType.Degree) { distanceMetric = UnitOfMeasure.ConvertDegreeTo(distance, UnitOfMeasure.EUnitType.Meter); } else { distanceMetric = distance; } var km = (int)Math.Truncate(distanceMetric / 1000); var mt = distanceMetric - 1000 * km; bool hasMeters; if (showCentimeter) { hasMeters = mt > 0; } else { hasMeters = Convert.ToInt32(mt) > 0; } if (hasMeters) { var formatMetter = showCentimeter ? "F2" : "F0"; if (km > 0) { return(string.Format("{0} Km {1:" + formatMetter + "} metros", km, mt)); } return(string.Format("{0:" + formatMetter + "} metros", mt)); } return($"{km} Km"); }