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
Java Book
Runnable examples
Data Type Int:
- Create Integer from int value
- Convert Integer to Binary String
- Convert int to String
- Convert octal number to decimal number
- Convert binary number to decimal number
- Convert decimal integer to octal number
- Convert decimal integer to hexadecimal number
- Convert hexadecimal number to decimal number
- Convert integer to readable format
- Convert Integer object to Numeric primitive types
- Shift int left
- Shift int Right
- Unsigned shift int right
- Min and Max values of datatype int
- Int Overflow
- Add two integers, checking for overflow.