Convert integer to readable format

 
import java.text.DecimalFormat;

public class Main {
  public static void main(String[] argv) {
    System.out.println(humanReadableInt(123123321123L));
  }

  public static String humanReadableInt(long number) {

    long absNumber = Math.abs(number);
    double result = number;
    String suffix = "";
    if (absNumber < 1024) {
      // nothing
    } else if (absNumber < 1024 * 1024) {
      result = number / 1024.0;
      suffix = "k";
    } else if (absNumber < 1024 * 1024 * 1024) {
      result = number / (1024.0 * 1024);
      suffix = "m";
    } else {
      result = number / (1024.0 * 1024 * 1024);
      suffix = "g";
    }
    DecimalFormat oneDecimal = new DecimalFormat("0.0");
    return oneDecimal.format(result) + suffix;
  }
}
  
Home 
  Java Book 
    Runnable examples  

Data Type Int:
  1. Create Integer from int value
  2. Convert Integer to Binary String
  3. Convert int to String
  4. Convert octal number to decimal number
  5. Convert binary number to decimal number
  6. Convert decimal integer to octal number
  7. Convert decimal integer to hexadecimal number
  8. Convert hexadecimal number to decimal number
  9. Convert integer to readable format
  10. Convert Integer object to Numeric primitive types
  11. Shift int left
  12. Shift int Right
  13. Unsigned shift int right
  14. Min and Max values of datatype int
  15. Int Overflow
  16. Add two integers, checking for overflow.