How to convert string into a number using AngularJS ?

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
    </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>

chevron_right


Output:



Example 2: The 2 strings are added and then converted to an integer and then added again.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
    </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>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.