Last active
October 31, 2016 11:03
-
-
Save hanihashemi/70bd76cede3e1e75cc951586a9f4c8fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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