Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am facing an issue of converting string value to numbers when i pass into my input field. I have used parseInt but instead getting an NAN value .

Here is my input field in html:

<div class="row">
 <div class="col-md-12">
 <div class="form-group col-md-6">
 <label>Form id:</label>
 <input type= "text" name="fromId" class="form-control" ng-model="IssueReceipt.formId" ng-required="true" readonly> <span class="error" ng-show="issueReceiptDataEntryform.fromId.$dirty && issueReceiptDataEntryform.fromId.$error.required "></span> 
                            </div>

And my script page where i pass the value :

$scope.getRandomForm = function() {
          paymentService.NewEReceipt().success(function(data){
                console.log(data)
                $scope.IssueReceipt.formId = parseInt(data);
                console.log(parseInt(data));
             })

        }
        $scope.getRandomForm()

Let me show u the console i'm getting :

enter image description here

If i use parseInt, i'm getting NAN as the value instead of number. Please can anyone help me with this ?

share|improve this question
    
Try triming the data before parsing to int. I dont see something wrong with the code as well as the value. – Jaganathan Bantheswaran 10 hours ago
    
@Jaganathan Bantheswaran The value i'm passing is an API value . The data in the controller i'm passing consists of values in it. – anil chean 9 hours ago
    
@Jaganathan Bantheswaran I'm getting the values . but, it is in the form of string .but, i need it in numbers. – anil chean 9 hours ago
    
I aware that data is string. The reason i told you to trim the string data is to avoid the space if any in the string. Try like this parseInt(data.trim()) – Jaganathan Bantheswaran 9 hours ago
    
@Jaganathan Bantheswaran i tried but still getting as NAN value . – anil chean 9 hours ago

Its working fine for me. I have hard coded the value in controller instead of data. You can send ur entire code so that I can check or just trim the data or debug and check it might be a simple issue

$scope.getRandomForm = function() {

      var data = "12345";
      $scope.IssueReceipt.formId = parseInt("12345");
       console.log(parseInt(data));


    }
    $scope.getRandomForm();

i am getting output as 12345

share|improve this answer
    
@ Ajay kumar The value i'm passing is an API value . The data in the controller i'm passing consists of values in it. – anil chean 9 hours ago

First convert the response data into string and then parse it into integer this might be help try once

 paymentService.NewEReceipt().success(function(data){
                    console.log(data)
                  var returnId = JSON.stringify(data);
                    $scope.IssueReceipt.formId = parseInt(returnId);
                    console.log($scope.IssueReceipt.formId);
                 })
share|improve this answer
    
still i'm getting an NAN value – anil chean 9 hours ago
    
data is already of type string. You dont want to convert that – Jaganathan Bantheswaran 9 hours ago
    
Try json.parse and then convert to int – Sujithrao 9 hours ago
    
@ Sujithrao is this the method you are telling paymentService.NewEReceipt().success(function(data){ console.log(data) $scope.IssueReceipt.formId = data; return JSON.parse(data); console.log(JSON.parse(data)); }) – anil chean 9 hours ago
    
@Sujithrao the API method that i'm using is an Post method . – anil chean 9 hours ago

Got an answer finally

 paymentService.NewEReceipt().success(function(data){
                //console.log(parseInt(data.trim()))
                $scope.IssueReceipt.formId = JSON.parse(data);
                //return JSON.parse(data);
                console.log(JSON.parse(data));
             })

Have used $scope.IssueReceipt.formId = JSON.parse(data);which converts string to numbers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.