How to convert string into a number using AngularJS ? - GeeksforGeeks

30 Sep, 2020
In this article, we will see how to convert a string into a number with the help of AngularJS.
Approach: 
The parseInt() method is used for converting the string to an integer. 
We will check whether the string is an integer or not by the isNumber() method.
Example 1: In the first example, the string ’90’ is converted to an integer.
HTML
<!DOCTYPE HTML><html>  <head>    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">    </script>      <script>        var myApp = angular.module("app", []);        myApp.controller("controller", function ($scope) {            $scope.a = '90';            $scope.isNumberA = angular.isNumber($scope.a);            $scope.convertToInt = function () {                $scope.a = parseInt($scope.a);                $scope.isNumberA = angular.isNumber($scope.a);            };        });    </script></head>  <body style="text-align:center;">    <h1 style="color:green;">        GeeksForGeeks    </h1>        <p>        Convert string into a         number in AngularJS    </p>        <div ng-app="app">        <div ng-controller="controller">            Value = {{a}}            <br>            <br>            <button ng-click='convertToInt()'>                Click to convert            </button>            <br>            <br>            isNumber(value) - {{isNumberA}}<br>        </div>    </div></body>  </html>
Output:
Example 2: The 2 strings are added and then converted to an integer and then added again.
HTML
<!DOCTYPE HTML><html>  <head>    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">    </script>      <script>        var myApp = angular.module("app", []);        myApp.controller("controller", function ($scope) {            $scope.a = "10";            $scope.b = "20";            $scope.strToInt = function () {                $scope.a = parseInt($scope.a);                $scope.b = parseInt($scope.b);            };        });    </script></head>  <body style="text-align:center;">    <h1 style="color:green;">        GeeksForGeeks    </h1>      <p>        Convert string into a         number in AngularJS    </p>        <div ng-app="app">        <div ng-controller="controller">            Value-1 = {{a}}            <br>            Value-2 = {{b}}            <br><br>            <button ng-click='strToInt()'>                Click to convert            </button>            <br><br>            sum - {{a + b}}<br>        </div>    </div></body>  </html>
Output:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
AngularJS-Misc
HTML-Misc
AngularJS
HTML
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 10 Angular Libraries For Web Developers
Angular PrimeNG Dropdown Component
Angular 10 (blur) Event
How to make a Bootstrap Modal Popup in Angular 9/8 ?
How to create module with Routing in Angular 9 ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
How to set the default value for an HTML <select> element ?
How to update Node.js and NPM to next version ?
How to set input type date in dd-mm-yyyy format using HTML ?