Math Functions in Angular2 Bindings

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

1

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

2

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

See all the rounding options in

For all the built-in pipes, check

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.