0

I'm trying to make a calculator app and i want to erase the decimal at the end when the result is not a decimal number but, when the result has a decimal number it shows.

Image Number

This Is My Code:

    String equation = "0";
    String result = "0";
    String expression = "";
    double equationFontSize = 38.0;
    double resultFontSize = 48.0;
    
    buttonPressed(String buttonText){
    setState(() {
      if(buttonText == "AC"){
        equation = "0";
        result = "0";
        equationFontSize = 38.0;
        resultFontSize = 48.0;
      } else if(buttonText == "⌫"){
         equationFontSize = 48.0;
         resultFontSize = 38.0;
        equation = equation.substring(0, equation.length - 1);
        if(equation == ""){
          equation = "0";
        }
      } else if (buttonText == "="){
        equationFontSize = 38.0;
        resultFontSize = 48.0;

        expression = equation;
        expression = expression.replaceAll('×', '*');
        expression = expression.replaceAll('÷', '/');
        expression = expression.replaceAll('^', '^');
        expression = expression.replaceAll('%', '%');

        try {
          Parser p = Parser();
          Expression exp = p.parse(expression);
          ContextModel cm = ContextModel();
          result = "${exp.evaluate(EvaluationType.REAL, cm)}";
        } catch(e) {
          result = "Error";
        }
      } else if(buttonText == "⌞⌝") {
        SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
      } else {
        equationFontSize = 48.0;
        resultFontSize = 38.0;
        if(equation == "0"){
          equation = buttonText;
        } else {
          equation = equation + buttonText;
        }
      }
    });
  }

    Container(
                      alignment: Alignment.centerRight,
                      padding: EdgeInsets.fromLTRB(10, 20, 10, 0),
                      child: Text(
                        equation,
                        style: GoogleFonts.workSans(
                            fontSize: equationFontSize,
                            fontWeight: FontWeight.w400),
                      ),
                    ),
                    Container(
                      alignment: Alignment.centerRight,
                      padding: EdgeInsets.fromLTRB(10, 20, 10, 0),
                      child: Text(
                        result,
                        style: GoogleFonts.workSans(
                            fontSize: resultFontSize,
                            fontWeight: FontWeight.w600),
                      ),
                    ),
jamesdlin
  • 81,374
  • 13
  • 159
  • 204

0 Answers0