Skip to content

Instantly share code, notes, and snippets.

@hanihashemi
Last active October 31, 2016 11:03
Show Gist options
  • Save hanihashemi/70bd76cede3e1e75cc951586a9f4c8fa to your computer and use it in GitHub Desktop.
Save hanihashemi/70bd76cede3e1e75cc951586a9f4c8fa to your computer and use it in GitHub Desktop.
/**
* Created by Elijah Saounkine
* Modified by Hani
*/
public class NumbersAbbreviationHelper {
private char[] c = new char[]{'K', 'M', 'B', 'T'};
public String makeItFriendly(double n) {
return makeItFriendly(n, 0);
}
private String makeItFriendly(double n, int iteration) {
if (n < 1000)
return String.format("%s", (long) n);
double d = ((long) n / 100) / 10.0;
boolean isRound = (d * 10) % 10 == 0;
return (d < 1000 ? ((d > 99.9 || isRound || (d > 9.99) ? (int) d * 10 / 10 : d + "") + "" + c[iteration]) : makeItFriendly(d, iteration + 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment