81

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

tanguy_k
  • 11,307
  • 6
  • 54
  • 58
Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63
  • 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. – shaunhusain Jan 28 '14 at 06:24

12 Answers12

95

In your controller:

$scope.num_str = parseInt(num_str, 10);  // parseInt with radix
Steve Eisner
  • 2,005
  • 23
  • 35
rg88
  • 20,742
  • 18
  • 76
  • 110
  • 9
    Remember in javascript to always specify the radix for parseInt http://stackoverflow.com/questions/850341/how-do-i-work-around-javascripts-parseint-octal-behavior – Steve Eisner Feb 27 '14 at 18:05
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!

Simon Wicki
  • 4,029
  • 4
  • 22
  • 25
hassassin
  • 5,024
  • 1
  • 29
  • 38
58

You can try:

{{ 1 * num_str + 1 }}

http://jsfiddle.net/Z32fP/

CD..
  • 72,281
  • 25
  • 154
  • 163
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

Mosho
  • 7,099
  • 3
  • 34
  • 51
20
{{ num_str - 0 }}

...works for me.

Vadim Panov
  • 341
  • 2
  • 7
  • 1
    Genius solution. When num_str = '' Null string this still works. I thought I would get a NaN but works awesome! – Oliver Aug 22 '17 at 09:58
  • 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. – John Churchill Mar 22 '21 at 15:31
  • Nice one, funny that {{ num_str + 1 }} doesn't work -.- – RASMiranda Feb 16 '23 at 14:14
11

None of the above worked for me.

But this did:

{{ (num1_str * 1) + (num2_str * 1) }}
D. Kermott
  • 1,613
  • 17
  • 24
7

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

var num=Number (num_str);
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
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.

sunderls
  • 760
  • 7
  • 8
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.

Anthue
  • 49
  • 3
1

Use in component.ts file

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

Use in component.html

convertStringToInt("2.4")
Deepak Jha
  • 359
  • 2
  • 10
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);
});
isherwood
  • 58,414
  • 16
  • 114
  • 157
webdev5
  • 467
  • 1
  • 8
  • 22
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>