Open In App

How to convert string into a number using AngularJS ?

Last Updated : 01 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

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>
    <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




<!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>
    <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:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads