Skip to content
Related Articles

Related Articles

How to convert string into a number using AngularJS ?
  • Last Updated : 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=
    </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=
    </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:

My Personal Notes arrow_drop_up
Recommended Articles
Page :