You can try the following:
String.format("%.2fM", yourNumber/ 1000000.0);
Numbers in the format will be displayed here
1,000,000 => 1.00M 1,234,567 => 1.23M
EDIT: -
I know its later editing, but yes there is another way:
private static String[] suff = new String[]{"","k", "m", "b", "t"}; private static int MAX_LENGTH = 4; private static String numberFormat(double d) { String str = new DecimalFormat("##0E0").format(d); str = str.replaceAll("E[0-9]", suff[Character.getNumericValue(str.charAt(str.length() - 1)) / 3]); while(str.length() > MAX_LENGTH || str.matches("[0-9]+\\.[az]")){ str = str.substring(0, str.length()-2) + str.substring(str.length() - 1); } return str; }
Call this function and you will get the result as follows:
201700 = 202k 3000000 = 3m 8800000 = 8.8m
source share