Search code examples
javaintdoubleprimitive

Java method returns double | int


I need a method that will accept String number as a parameter and return double if it has a remainder, or int if it is decimal.

I wrote such code:

 private double convertToNumber(String number) {
    double d = Double.parseDouble(number);
    if (d % 1.0 == 0) {
        return Integer.parseInt(number);
    } else {
        return d;
    }
}

but as signature of this method is double it returns double in any case.

Please give me some hint.


Solution

  • I'm not quire sure I understand the intention of returning a double or and int knowing that's not really possible for a single function to return different data types.

    What I think I understand from your sample code, is that if a String (For example, "123.0" doesn't matter how many trailing zeros) is passed into the CalculatorEngine, from the link you provided, you want it to be treated as an int and for the math operator to perform calculations with an int and a double (since currentTotal is a double).

    Now after all the calculations are done, if currentTotal ends with a .0 then you probably want that treated as an int also and drop the point, otherwise keep the point.

    If I'm understanding this correctly, here's what I think CalculatorEngine might look like:

    public static void main(String[] args) {
        CalculatorEngine ce = new CalculatorEngine();
        ce.equal("1.01");
        ce.add("12");
        System.out.println(ce.getTotalString());
        ce.subtract("0.01");
        System.out.println(ce.getTotalString());
        ce.multiply("100.00000");
        System.out.println(ce.getTotalString());
        ce.divide("123");
        System.out.println(ce.getTotalString());
    }
    
    public static class CalculatorEngine {
        private enum Operator {
            ADD, SUBTRACT, MULTIPLY, DIVIDE
        }
    
        private double currentTotal;
    
        public CalculatorEngine() {
            currentTotal = 0;
        }
    
        public String getTotalString() {
            return currentTotal % 1.0 == 0 
                    ? Integer.toString((int)currentTotal) 
                    : String.valueOf(currentTotal);
        }
    
        public void equal(String number) {
            currentTotal = Double.parseDouble(number);
        }
    
        public void add(String number) {
            convertToDouble(number, Operator.ADD);
        }
    
        public void subtract(String number) {
            convertToDouble(number, Operator.SUBTRACT);
        }
    
        public void multiply(String number) {
            convertToDouble(number, Operator.MULTIPLY);
        }
    
        public void divide(String number) {
            convertToDouble(number, Operator.DIVIDE);
        }
    
        public void changeSign(String number) {
            Double d = Double.parseDouble(number);
            currentTotal = d * (-1);
        }
    
        public void dot(String number) {
            // todo
        }
    
        private boolean isDouble(String number) {
            double d = Double.parseDouble(number);
            return d % 1.0 != 0;
        }
    
        private void convertToDouble(String number, Operator operator) {
            double dblNumber = Double.parseDouble(number);
            switch (operator) {
                case ADD:
                    add(dblNumber);
                    break;
                case SUBTRACT:
                    subtract(dblNumber);
                    break;
                case MULTIPLY:
                    multiply(dblNumber);
                    break;
                case DIVIDE:
                    divide(dblNumber);
                    break;
                default:
                    throw new AssertionError(operator.name());   
            }
        }
    
        private void add(double number) {
            currentTotal += number % 1.0 == 0 ? (int)number : number;
        }
    
        private void subtract(double number) {
            currentTotal -= number % 1.0 == 0 ? (int)number : number;
        }
    
        private void multiply(double number) {
            currentTotal *= number % 1.0 == 0 ? (int)number : number;
        }
    
        private void divide(double number) {
            currentTotal /= number % 1.0 == 0 ? (int)number : number;
        }
    }
    

    Results:

    13.01
    13
    1300
    10.56910569105691