> To show potential intelligence, you could have refrained from starting a

new thread and you could have indented your code, as is customary, to

show structure.
*** OK ** but potential intelligence may be too fleeting for me

You could also have tested it.
*** I did, but you did a much better job at that.

To test whether the first character of a string is 5 or more, knowing

that it will be in 0..9, one might use S.charAt()>="5" - it seems OK

with an empty string - undertested.
*** I wanted to see if the string, if sorted alphabetically, would come on

or after a "5.

Missing } shows carelessness.

*** yeah, When I copied it from the web page I tested it on, the "}" didn't

make the copy buffer. I inadvertantly left it off.

****

Thanks for taking the time to test the code.

The bad output examples you pointed out helped a lot in debugging.

The revised code fixes those errors, and I also tested the function with

ramdom numbers and random 'places', and seems to be working now. (I wrote a

function to test it 100 times and it looks ok.)

(I don't know what it was about ".07", but adding zeroes to the end of the

string fixed that part. I think an empty string must be rounding to 1.)

**

***** Here's the revised code **

// the args: n is the number to round, and x the number of places

// This function formats to x decimal places - padding with trailing

// zeroes if required

function roundNtoXplaces(n, x){

var itWasNegative = false;

if (n < 0){

n*=-1;

itWasNegative = true;

}

// move the decimal over x places

n = n * (Math.pow(10, x));

// coerce the number to a string

n = "0" + n;

//** to assure there is something after the deci9mal to work with

if (n.indexOf(".") < 0){

n += ".0000"

}

// split the string at the new decimal place

var numParts = n.split(".");

//** to avoid empty array, add zeroes to the end of the string***

numParts[1] += "" + "00000";

// if the decimal part would come after "5" if sorted

// then add 1 to the int part

if (numParts[1]+"" >= "5"){

numParts[0]++;

}

// coerce the number back to a string

// This string contains the numbers we will be returning

numParts[0] = "" + numParts[0];

//get the length of the string

var len = numParts[0].length;

// since we moved the decimal over , now we put itback

// intPart is the part before the decimal

var intPart = numParts[0].substr(0, (len - x));

intPart *=1; // removes leading zeroes

//decimalPart is the part of the string after the decimal

var decimalPart = numParts[0].substr( len - x, x );

if (itWasNegative){

intPart *=-1;

}

var outString = intPart + "." + decimalPart;

return outString;

}// Here's that pesky closing curly-brace ;-)