Assuming a maximum value of x you could do this:

var x=1234567890; //the number to be evaluated

var e=0; //to accumulate the scale

var i=x; //a working variable

//Find the number's scale or order of decimal magnitude

while (i > 1) {

i=i/10;

e++;

}

//Once you have determined the scale for the maximum value in the set, treat all members of the set the same way.

if (e>=9) {

x=x/1E9+"B";

}else if (e>=6) {

x=x/1E6+"M";

}else if (e>=3) {

x=x/1E3+"K";

}

alert(x);