Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math functions in angular2 bindings

Is there a way to use math functions in angular2 bindings?

example

<div class="partition-panel">
                <b class="pull-left">{{Math.round(variable/12*2)}}</b>
                <b class="pull-right">{{Math.round(variable/12*2)}}</b>
 </div>

when try to use this i got error

Cannot read property 'round' of undefined

Also the similar question is answered for angular1

like image 862
Jagadeesh Govindaraj Avatar asked Nov 16 '16 12:11

Jagadeesh Govindaraj


2 Answers

You can try this :

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{math.round(number)}}</h2>
    </div>
  `,
})
export class App {
  number = 2.5;
  math = Math;
}

DEMO

like image 82
Adrien BARRAL Avatar answered Oct 24 '22 09:10

Adrien BARRAL


For rounding numbers in Angular templates, you can use the DecimalPipe: {{ value | number }}

See all the rounding options in https://angular.io/api/common/DecimalPipe

For all the built-in pipes, check https://angular.io/api?type=pipe

like image 23
Anis Abboud Avatar answered Oct 24 '22 10:10

Anis Abboud