// modified to handle negatives and to use decimal notation for numbers
// greater than 0.001 and less than 1000
accuracy = 2; 
maxAccuracy=7;

function isDigit(ch) {
    if ((ch == '0') || (ch == '1') || (ch == '2') || (ch == '3') || (ch == '4') || 
        (ch == '5') || (ch == '6') || (ch == '7') || (ch == '8') || (ch == '9'))
        { return true; }
    return false;
}

function roundAtDecimalPosition(number,accuracy) {
    return Math.round(number * Math.pow(10,accuracy)) / Math.pow(10,accuracy);
}

function extendToDecimalPosition(n,accuracy) {
    retString = "" + n;
    pos = 0;
    while ((retString.charAt(pos) != '.') && (retString.charAt(pos) != '')) {
      pos++;
    }
    if ((retString.charAt(pos) == '') && (accuracy > 0)) 
        { retString = retString + "."; }
    for (x=0; x<accuracy; x++) {
        pos++;
        if (retString.charAt(pos) == '') 
            { retString = retString + "0"; }       
    }
    return retString;
}

function scientificNotation(number,accuracy) {
    NegFlag = "";
    n = number;
    if (n == 0) { return "0"; }
    if (n < 0 ) { 
        NegFlag = "-"
        }
    n = Math.abs(n)
    if ((n < 1000) && ( n > 0.001)) {return Math.round(number*10000)/10000}
    e = 0;
    done=false;
    while (!done) {
        while (Math.abs(n) >= 10) {
            n = n / 10;
            e++;
        }
        while (Math.abs(n) < 1) {
            n = n * 10;
            e--;
        }
        n = roundAtDecimalPosition(n,accuracy);
        if ((n >= 1) && (n < 10)) { done = true; }
    } 
    return NegFlag+extendToDecimalPosition(n,accuracy) + "e" + e;
}

