简体   繁体   中英

I can't get the answer proper answer in my Android Calculator

I had follow the steps to create my Calculator Apps, while I pressed 100+6%, it should be 106.0 but it showed me the final calculated answer was 100.06, is there something I missed? Thank you.

       Equal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            process = Callayout.getText().toString();

            process = process.replaceAll("%","/100");
            process = process.replaceAll("x","*");
            process = process.replaceAll("(6%)","*0.06");

            Context rhino = Context.enter();

            rhino.setOptimizationLevel(-1);

            String finalans = "";

            try{
                Scriptable scriptable = rhino.initStandardObjects();
                finalans = rhino.evaluateString(scriptable,process,"Javascript",1,null).toString();
            }catch(Exception e){
                finalans = "0";
            }

            Callayout.setText(finalans);
        }
    });

The / operator takes precedence over + . The expression you entered is equal to 100+(6/100) . Also, you don't need this line: process = process.replaceAll("(6%)","*0.06"); , because all the % have already been replaced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM