src/main/java/com/acme/calculator/main/Calculator.java
/**
     * Parses an expression, performs the specified calculations in the expression, and returns a String result.
     *
     * @param expression the expression to parse and calculate
     * @return String result of the expression; can be a descriptive error message
     */
Parses an expression, performs the specified calculations in the expression, and returns a String result.
@param expression the expression to parse and calculate
@return String result of the expression; can be a descriptive error message
Parses an expression performs the specified calculations in the expression and returns a String result . @param expression the expression to parse and calculate @return String result of the expression ; can be a descriptive error message
public String calculate(final String expression) {
        logger.info("calculate()");
        logger.debug("calculate() expression: '{}'", expression);
        String result = null;
        try {
            BigDecimal value = parse(expression);
            result = value.toEngineeringString();
            if (result.contains(PERIOD) && result.endsWith(ZERO)) {
                result = value.stripTrailingZeros().toEngineeringString();
                logger.debug("used stripTrailingZeros() on output");
            }
        } catch (Throwable xcptn) {
            final String errorMsg = String.format("%s '%s'", ERROR_PREFIX, expression);
            logger.error(errorMsg, xcptn);
            result = errorMsg + " (" + xcptn.toString() + ")";
        }
        return result;
    }
public String calculate ( final String expression ) { logger . info ( "calculate()" ) ; logger . debug ( "calculate() expression: '{}'" , expression ) ; String result = null ; try { BigDecimal value = parse ( expression ) ; result = value . toEngineeringString ( ) ; if ( result . contains ( PERIOD ) && result . endsWith ( ZERO ) ) { result = value . stripTrailingZeros ( ) . toEngineeringString ( ) ; logger . debug ( "used stripTrailingZeros() on output" ) ; } } catch ( Throwable xcptn ) { final String errorMsg = String . format ( "%s '%s'" , ERROR_PREFIX , expression ) ; logger . error ( errorMsg , xcptn ) ; result = errorMsg + " (" + xcptn . toString ( ) + ")" ; } return result ; }
Parses an expression, performs the specified calculations in the expression, and returns a String result.
Parses an expression performs the specified calculations in the expression and returns a String result .
// Remove trailing zeros so 1/16 + 1/16 -> 0.125 (instead of 0.1250) // and 2.000...0 becomes "2" // For the integer value case, BigDecimal.toBigIntegerExact() can also be used to remove trailing zeros.