Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static char[] c = new char[]{'k', 'm', 'b', 't'};
- /**
- * Recursive implementation, invokes itself for each factor of a thousand, increasing the class on each invokation.
- * @param n the number to format
- * @param iteration in fact this is the class from the array c
- * @return a String representing the number n formatted in a cool looking way.
- */
- private static String coolFormat(double n, int iteration) {
- double d = ((long) n / 100) / 10.0;
- boolean isRound = (d * 10) %10 == 0;//true if the decimal part is equal to 0 (then it's trimmed anyway)
- String return1 = (d < 1000? //this determines the class, i.e. 'k', 'm' etc
- ((d > 99.9 || isRound || (!isRound && d > 9.99)? //this decides whether to trim the decimals
- (int) d * 10 / 10 : d + "" // (int) d * 10 / 10 drops the decimal
- ) + "" + c[iteration])
- : coolFormat(d, iteration+1));
- if(return1.startsWith("0")){
- return (int) n+"gp";
- }
- return return1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement