Skip to main content
put comment in code block
Source Link
demongolem
  • 9.8k
  • 36
  • 97
  • 107

//code longer but work sure...

//code longer but work sure... public static String formatK(int number) { if (number < 999) { return String.valueOf(number); } if (number < 9999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 1); String str2 = strNumber.substring(1, 2); if (str2.equals("0")) { return str1 + "k"; } else { return str1 + "." + str2 + "k"; } } if (number < 99999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 2); return str1 + "k"; } if (number < 999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 3); return str1 + "k"; } if (number < 9999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 1); String str2 = strNumber.substring(1, 2); if (str2.equals("0")) { return str1 + "m"; } else { return str1 + "." + str2 + "m"; } } if (number < 99999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 2); return str1 + "m"; } if (number < 999999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 3); return str1 + "m"; } NumberFormat formatterHasDigi = new DecimalFormat("###,###,###"); return formatterHasDigi.format(number); } 

//code longer but work sure...

public static String formatK(int number) { if (number < 999) { return String.valueOf(number); } if (number < 9999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 1); String str2 = strNumber.substring(1, 2); if (str2.equals("0")) { return str1 + "k"; } else { return str1 + "." + str2 + "k"; } } if (number < 99999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 2); return str1 + "k"; } if (number < 999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 3); return str1 + "k"; } if (number < 9999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 1); String str2 = strNumber.substring(1, 2); if (str2.equals("0")) { return str1 + "m"; } else { return str1 + "." + str2 + "m"; } } if (number < 99999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 2); return str1 + "m"; } if (number < 999999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 3); return str1 + "m"; } NumberFormat formatterHasDigi = new DecimalFormat("###,###,###"); return formatterHasDigi.format(number); } 
//code longer but work sure... public static String formatK(int number) { if (number < 999) { return String.valueOf(number); } if (number < 9999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 1); String str2 = strNumber.substring(1, 2); if (str2.equals("0")) { return str1 + "k"; } else { return str1 + "." + str2 + "k"; } } if (number < 99999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 2); return str1 + "k"; } if (number < 999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 3); return str1 + "k"; } if (number < 9999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 1); String str2 = strNumber.substring(1, 2); if (str2.equals("0")) { return str1 + "m"; } else { return str1 + "." + str2 + "m"; } } if (number < 99999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 2); return str1 + "m"; } if (number < 999999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 3); return str1 + "m"; } NumberFormat formatterHasDigi = new DecimalFormat("###,###,###"); return formatterHasDigi.format(number); } 
Source Link

//code longer but work sure...

public static String formatK(int number) { if (number < 999) { return String.valueOf(number); } if (number < 9999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 1); String str2 = strNumber.substring(1, 2); if (str2.equals("0")) { return str1 + "k"; } else { return str1 + "." + str2 + "k"; } } if (number < 99999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 2); return str1 + "k"; } if (number < 999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 3); return str1 + "k"; } if (number < 9999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 1); String str2 = strNumber.substring(1, 2); if (str2.equals("0")) { return str1 + "m"; } else { return str1 + "." + str2 + "m"; } } if (number < 99999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 2); return str1 + "m"; } if (number < 999999999) { String strNumber = String.valueOf(number); String str1 = strNumber.substring(0, 3); return str1 + "m"; } NumberFormat formatterHasDigi = new DecimalFormat("###,###,###"); return formatterHasDigi.format(number); }