Java tutorial
//package com.java2s; //License from project: Open Source License import java.text.DecimalFormat; public class Main { public static final char numberSuffixes[] = new char[] { ' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' }; public static String getCompressedNumberString(int num) { DecimalFormat df = new DecimalFormat("0.0"); double tg = num; int si = 0; while (tg > 1000) { tg /= 1000.0; si++; } if ((int) tg == num) { df = new DecimalFormat("0"); } else { df = new DecimalFormat("0.0"); } StringBuilder sb = new StringBuilder(); sb.append(df.format(tg)); sb.append(numberSuffixes[si]); return sb.toString(); } }