5

I am looking for a pipe that transforms like below, just wanted to round 2 decimal points.

I tried [ngModel]="item.value | number number:'1.0-X'", but it's not working in all cases

239.779 > 239.78
0.674 > 0.67
35355.453 > 35355.45
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Jayesh
  • 641
  • 4
  • 13
  • 32

2 Answers2

16

You can use DecimalPipe.

x.x-x minIntegerDigits: The minimum number of integer digits before the decimal point. Default is 1.

x.x-x minFractionDigits: The minimum number of digits after the decimal point. Default is 0.

x.x-x maxFractionDigits: The maximum number of digits after the decimal point. Default is 3.

For exactly two decimals and at least one number before decimal you can write like this:

[ngModel]="item.value | number:'1.2-2'"

or since minIntegerDigits default is 1: [ngModel]="item.value | number:'.2-2'"

If you are interested in locale-specific configurations you can read this article:

Michael Karén
  • 1,105
  • 9
  • 13
1

Seperate the banana binding to: event (ngModelChange) and property [ngModel] parts (ngModelChange)="Rate1=$event" [ngModel]="Rate1 | number:'1.2-2' ".

where 1.2-2 is as follows:
Decimal representation options, specified by a string in the following format: {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}.

You may use it according to your requirements.

Also refer to- https://stackblitz.com/edit/decimal-pipe-example?file=app%2Fapp.component.html