The below code solves the specified question very well, but in cases of 5 digits it does not. For example, it does well with this scenario 1345 to 1.3k, but it does not work with 13547 to 13.5k please help me out This is the code below
private static final NavigableMap<Long, String> suffixes = new TreeMap<>();
static {
suffixes.put(1_000L, "k");
suffixes.put(1_000_000L, "M");
suffixes.put(1_000_000_000L, "G");
suffixes.put(1_000_000_000_000L, "T");
suffixes.put(1_000_000_000_000_000L, "P");
suffixes.put(1_000_000_000_000_000_000L, "E");
}
public static String format(long value) {
if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1);
if (value < 0) return "-" + format(-value);
if (value < 1000) return Long.toString(value);
Map.Entry<Long, String> e = suffixes.floorEntry(value);
Long divideBy = e.getKey();
String suffix = e.getValue();
long truncated = value / (divideBy / 10);
boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 100);
return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix;
}
}