How to convert string into a number using AngularJS ?
Last Updated :
01 Aug, 2022
Improve
In this article, we will see how to convert a string into a number in AngularJS, along with understanding its implementation through the illustrations.
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
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 > < h3 > Convert string into a number in AngularJS </ h3 > < 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}} </ div > </ div > </ body > </ html > |
Output:

Example 2: The 2 strings are added and then converted to an integer and then added again.
- HTML
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 > < h3 > Convert string into a number in AngularJS </ h3 > < 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:
