function fn(a) { var n = 0; while (a > 1000) { a /= 1000; n++; } return +a.toFixed(1) + (["", "k", "m", "b", "t"][n] || ""); } alert(fn(145)); alert(fn(145800)); alert(fn(6458300));