Angular Calculator Application

In this guide, we will show you how to create a simple & basic calculator application in Angular. If you are new to Angular you can take a look at our Angular tutorial.

Create Calculator Application

The first step is to create a new calculator application. We name it as ngCalculator. You can refer to the tutorial How to Create a new project in Angular

Say yes to CSS & Routing.

Add Bootstrap 4

We will use the bootstrap 4 to style our app. Copy the following code to the index.html

Create Calculator Component

Create the calculator.component.ts & calculator.component.html under the folder calculator

Template

Now open the calculator.component.html

Our final screen is going to look as follows

  • The first row displays the user input including the operators.
  • The second row displays the result as the user type.
  • C Clears all displays
  • x clears the last input i.e delete
  • /, X,- & + are our four operators for division, multiplication subtraction & addition
  • use . to enter the decimal numbers
  • = Calculates and resets the user input.

The Template code is as follows.

We are using event binding to map user clicks to component class.

The pressNum() captures the number & decimal buttons. The pressOperator() method captures the /, X,- & + button clicks. allClear clears all the inputs. clear() deletes the last input. getAnswer() calculates and resets the Calculator again.

Component

The component is where we put the calculator logic. We use the selector: 'ng-calculator', so that we can use the calculator component any where in our code using the <ng-calculator></ng-calculator>.

Angular Calculator Logic

Every time user click on a number or operand, we append it to the input variable. For example, if user types 54+200+20, we assign it the input variable. It makes it easier to use the built in JavaScript function eval to calculate the result just by invoking eval(input).

We need to take care of few issues. For Example user may repeat the operands (54++23). Or a invalid decimal number like 54.23.5 etc. Also eval will throw an error if the number begins with 0.


Best Angular Books
The Top 8 Best Angular Books, which helps you to get started with Angular  

pressNum captures the numbers & decimal points . and appends it to the input variable. There are two validations here.

  1. A number cannot contain more than one decimal place.
  2. Eval will throw the error (Octal literals are not allowed in strict mode.), if the number begins with 0. Hence we remove it.

getLastOperandreturns the last operand. For example, if the user input is 120+57 then the last operand is 57

pressOperator captures the operators. It again appends it to the input variable. We make sure that that operator is followed by a number and not another operator (54++)

clear removes the last input.

allClear clears everything.

calcAnswer is where the calculation happens. We check the last character. If it is a decimal point (.) then we remove it (Example in 95+23.). We also remove the last character if it is an operator. (Example 95+23+).

getAnswer calculates and assigns it to the result to input variable.

That’s it.

Now open the app.component.html and add <ng-calculator></ng-calculator>. Also do not forget to add the CalculatorComponent in the declaration array of app.module.ts.

Angular Calculator Example

Next Steps

You can improve the calculator and add operators like % & ± etc. Also try using the ( & ).

You can also avoid using the eval function. In that case you can make use of the algorithm to evaluate arithmetic expressions & Shunting-yard algorithm etc. Also, refer to the ng-calculator

Reference

  1. Eval
  2. algorithm to evaluate arithmetic expressions
  3. Shunting-yard algorithm
  4. ng-calculator

3 thoughts on “Angular Calculator Application”

  1. Error: src/app/calculator/calculator.component.ts:22:28 – error TS2339: Property ‘getLastNumber’ does not exist on type ‘CalculatorComponent’.

    22 const lastNum=this.getLastNumber()

    How to solve it?
    Thanks

  2. Error: src/app/calcultor/app.calculator.component.ts:24:31 – error TS2339: Property ‘lastIndexOf’ does not exist on type ‘void’.

    24 console.log(lastNum.lastIndexOf(“.”))
    ~~~~~~~~~~~
    src/app/calcultor/app.calculator.component.ts:25:23 – error TS2339: Property ‘lastIndexOf’ does not exist on type ‘void’.

    25 if (lastNum.lastIndexOf(“.”) >= 0) return;
    ~~~~~~~~~~~

    How To resolve it?

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top