CodyKochmann
9/21/2016 - 9:11 PM

properly round a double to two decimal places in java

properly round a double to two decimal places in java

/* rounds a double to two decimals accurately */
private static double twoDecimals(double numberToTrim){
    boolean addOne = ((numberToTrim*100.0-(int)(numberToTrim*100.0)) >= 0.5);
    double output = ((int)(numberToTrim*100))/100.0;
    if(addOne){
        output+=0.1;
    }
    return output;
}