Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1.  
  2. private static char[] c = new char[]{'k', 'm', 'b', 't'};
  3.  
  4. /**
  5. * Recursive implementation, invokes itself for each factor of a thousand, increasing the class on each invokation.
  6. * @param n the number to format
  7. * @param iteration in fact this is the class from the array c
  8. * @return a String representing the number n formatted in a cool looking way.
  9. */
  10. private static String coolFormat(double n, int iteration) {
  11. double d = ((long) n / 100) / 10.0;
  12. boolean isRound = (d * 10) %10 == 0;//true if the decimal part is equal to 0 (then it's trimmed anyway)
  13. String return1 = (d < 1000? //this determines the class, i.e. 'k', 'm' etc
  14. ((d > 99.9 || isRound || (!isRound && d > 9.99)? //this decides whether to trim the decimals
  15. (int) d * 10 / 10 : d + "" // (int) d * 10 / 10 drops the decimal
  16. ) + "" + c[iteration])
  17. : coolFormat(d, iteration+1));
  18. if(return1.startsWith("0")){
  19. return (int) n+"gp";
  20. }
  21. return return1;
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement