84

A number string '5'

var num_str = '5';

How can I parseInt and let below answers correct at the same time?

{{num_str + 1}}  // 6
{{num_str - 1}}  // 4

parseInt can't be used in an Angular expression,

{{parseInt(num_str) - 1}}    

number filter can't do add and minus,

{{num_str - 1 | number}}

If anyone have useful suggestion, I will very appreciate of you

1
  • 2
    Sounds like you want a custom filter they're fairly easy to write just a function that takes the input and returns the output. Commented Jan 28, 2014 at 6:24

12 Answers 12

95

In your controller:

$scope.num_str = parseInt(num_str, 10);  // parseInt with radix
Sign up to request clarification or add additional context in comments.

1 Comment

Remember in javascript to always specify the radix for parseInt stackoverflow.com/questions/850341/…
80

I prefer to use an angular filter.

app.filter('num', function() {
    return function(input) {
      return parseInt(input, 10);
    };
});

then you can use this in the dom:

{{'10'|num}}

Here is a fiddle.

Hope this helped!

2 Comments

very nice solution!
Does not work if you want to +1 or -1 as the question asked.
58

You can try:

{{ 1 * num_str + 1 }}

http://jsfiddle.net/Z32fP/

3 Comments

Please don't ever add little hacks like this into a real code base.
This won't work if the number is in the thousands and the string has a comma in it. This could turn 1000 into a number but not 1,000
I agree that this is a hack, but this looks like the only working solution
24

Another option would be:

$scope.parseInt = parseInt;

Then you could do this like you wanted:

{{parseInt(num_str)-1}}

This is because angular expressions don't have access to the window, only to scope.

Also, with the number filter, wrapping your expression in parentheses works:

{{(num_str-1) | number}}

DEMO

1 Comment

Angular's built in | number filter is the way to go in most cases.
20
{{ num_str - 0 }}

...works for me.

3 Comments

Genius solution. When num_str = '' Null string this still works. I thought I would get a NaN but works awesome!
Nice solution. Works for null and blank string; this is just Javascript behavior so it should work permanently. Only gets NaN for undefined or the string is not a valid number.
Nice one, funny that {{ num_str + 1 }} doesn't work -.-
11

None of the above worked for me.

But this did:

{{ (num1_str * 1) + (num2_str * 1) }}

Comments

7

You can use javascript Number method to parse it to an number,

var num=Number (num_str);

1 Comment

Downvoted because this is a discussion about angularjs, not pure javascript.
2

Besides {{ 1 * num_str + 1}} You can also try like this(minus first):

{{ num_str - 0 + 1}}

But the it's very fragile, if num_str contains letters, then it will fail. So better should try writing a filter as @hassassin said, or preprocess the data right after initiating it.

Comments

1

You can create a Pipe and use it wherever you want in your system.

import { Pipe, PipeTransform } from '@angular/core';
 @Pipe({
     // tslint:disable-next-line:pipe-naming
     name: 'toNumber',
     pure: false }) export class ToNumberPipe implements PipeTransform { 
     public transform(items: any): any {
         if (!items) {
             return 0;
         }
         return parseInt(items, 10);
     } }

In the HTML

{{ attr.text | toNumber }}

Remember to declare this Pipe to be accessfull in your modules file.

Comments

1

Use in component.ts file

convertStringToInt(str){ 
  var Num = parseInt(str); 
  return Num;
}

Use in component.html

convertStringToInt("2.4")

Comments

0

I tried the solutions mentioned above and none of them worked for me. I used JSON.parse and it worked:

$http.get('/api/getAdPolling')
  .success(function (data) {
    console.log('success: ' + data.length);

    if (JSON.stringify(data) != "not found") {
        $scope.adPoll = JSON.parse(data);
    }
})
  .error(function (data) {
    console.log('Error: ' + data);
});

Comments

0

Not really great but a funny hack: You can -- instead of +

{{num_str -- 1 }}

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app>
  {{'1'--1}}
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.