I saw this question come up in a Facebook group about how to create a calculator. I thought there were many reasons this could be helpful to learn. Learning Context Variables and doing Arithmetic. Changing variables between numbers and texts also was a small issue to overcome.
So using a few context variables we created a basic calculator in Power Apps from scratch.
0:00 Introduction
0:30 Setting Up The Layout
5:20 Our First Context Variable varNumber
7:28 Clear Button
8:25 Adding If to varNumber Equation
10:20 Adding a second label to display Calculation
11:45 Our Second Context Variable varCalculate
12:35 Adding an equals button
13:46 Our Third Context Variable varToggle
15:15 Our Fourth Context Variable varCalcType
16:42 Our Answer for the Equals Equation
20:27 Updating our Clear Button
21:03 Updating the Equals Equation with varCalculate
22:21 Updating each Operation with +,-,*,/
22:55 Using a Switch Statement to either Add, Subtract, Multiply, or Divide
24:25 Looking back at each Equation
25:59 Changing varCalculate to say +,-,*,/ in Text
26:35 Conclusion
Numbers Formula:
If(varNumber = "0" || varToggle=true, UpdateContext({varNumber:"1"}),UpdateContext({varNumber:varNumber & 1}));
UpdateContext({varToggle:false})
Operation Formula:
UpdateContext({varCalculate: varNumber & " + "});
UpdateContext({varToggle:true});
UpdateContext({varCalcType:"Plus"})
Equals Formula:
UpdateContext({varCalculate: varCalculate & varNumber});
Switch(
varCalcType,
"Plus",
UpdateContext({varNumber: Text(Left(lblCalculate.Text,Len(lblCalculate.Text) - (Len(varNumber)+3)) + Value(lblNumber.Text))}),
"Subtract",
UpdateContext({varNumber: Text(Left(lblCalculate.Text,Len(lblCalculate.Text) - (Len(varNumber)+3)) - Value(lblNumber.Text))}),
"Multiply",
UpdateContext({varNumber: Text(Left(lblCalculate.Text,Len(lblCalculate.Text) - (Len(varNumber)+3)) * Value(lblNumber.Text))}),
"Divide",
UpdateContext({varNumber: Text(Left(lblCalculate.Text,Len(lblCalculate.Text) - (Len(varNumber)+3)) / Value(lblNumber.Text))})
);
UpdateContext({varToggle: true});