private String format(Double n) {
	    result = n;    // update the variable result
//	    if (!isError) {
	        String output = String.valueOf(n);
	        

	        // check 1/0 case (which leads to infinity)
	        if (Double.isInfinite(n)) {
	            isError = true;
	            canInvert = false;
	            result = Double.NaN;
	            equalCalled = true;
	            return "Error";
	        }

	        // check sqrt(-x) case (which leads to NaN)
	        if (Double.isNaN(n)) {
	            isError = true;
	            equalCalled = true;
	            return "Error";
	        }

	        // if the output contains a 'E', we cut the number of digits before 'E' to make the whole String
	        // display properly on screen
	        if (output.contains("E")) {
	            String[] tempArray;
	            tempArray = output.split("E");
	            if (Double.parseDouble(tempArray[1]) > 99 || Double.parseDouble(tempArray[1]) < -99) {
	                return "error";
	            }

	            tempArray[0] = tempArray[0].substring(0, Math.min(10, (tempArray[0].length())));  // here 10 is for the worst case of e-XX
	            output = tempArray[0] + "e+" + tempArray[1];
	        }

	        if (endZero.equals("")) {

	            if (output.endsWith(".0"))     // if ends with ".0", cut that
	                output = output.substring(0, Math.min(15, (output.length() - 2)));
	            else       // if ends with something else, we always want to keep the length below 15
	                output = output.substring(0, Math.min(15, (output.length())));

	        } else {

	            if (output.endsWith(".0")) 
	                output = output.substring(0, Math.min(15, (output.length() - 1)));
	            else
	                output = output.substring(0, Math.min(15, (output.length())));
	        }

	        output += endZero;
	        return output;
//	    }
//	    else return "Error";   // if isError then always output Error
	}