77

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
  • 10,220
  • 6
  • 49
  • 54
Chen-Tsu Lin
  • 21,992
  • 15
  • 51
  • 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

94

In your controller:

$scope.num_str = parseInt(num_str, 10);  // parseInt with radix
Steve Eisner
  • 2,015
  • 23
  • 34
rg88
  • 20,246
  • 18
  • 72
  • 109
  • 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
79

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
  • 3,939
  • 4
  • 19
  • 24
hassassin
  • 4,924
  • 1
  • 26
  • 37
58

You can try:

{{ 1 * num_str + 1 }}

http://jsfiddle.net/Z32fP/

CD..
  • 68,981
  • 24
  • 147
  • 156
23

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,071
  • 3
  • 32
  • 51
17
{{ num_str - 0 }}

...works for me.

Vadim Panov
  • 301
  • 2
  • 6
  • 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
11

None of the above worked for me.

But this did:

{{ (num1_str * 1) + (num2_str * 1) }}
D. Kermott
  • 1,482
  • 14
  • 21
7

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

var num=Number (num_str);
Jayantha Lal Sirisena
  • 20,846
  • 10
  • 70
  • 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
  • 672
  • 7
  • 7
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
  • 39
  • 3
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
  • 52,576
  • 15
  • 105
  • 143
webdev5
  • 457
  • 1
  • 8
  • 21
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>
0

Use in component.ts file

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

Use in component.html

convertStringToInt("2.4")
Deepak Jha
  • 214
  • 2
  • 10