I am trying to display number format in my angular 4 application. Basically what i am looking at is if the number is in 12.23 millions then it should display For e.g 12.2M (One decimal place)
If the number is 50,000.123 then 50.1K
How do i achieve that in angular. Do I need to write a directive ? Are there any inbuilt pipes in angular ?
structure
export interface NpvResults {
captiveInsYear: number[];
captiveInsPremiumPaid: number[];
captiveInsTaxDeduction: number[];
captiveInsLoanToParent: number[];
captiveInsCapitalContribution: number[];
captiveDividentDistribution: number[];
captiveInsTerminalValue: number[];
}
The array is initialized to the following value
this.sourceResults.captiveInsPremiumPaid = [1,2,3,4,5];
The html
<td *ngFor= "let item of NpvResults.captiveInsPremiumPaid" >{{item}}</td>
you can create PIPE
implement in the view
result
I write the below pipe, it will handle all of the negative scenarios( Negative numbers, Strings, blank) as well. And works best in all of the cases. Complete Article here.
The easiest way is to make a pipe and a service. You can do the actually formatting in service using numeral js library and import that service in the pipe and apply formatting in transform method.
Here I just give you an idea first create
Html
{{number | shortNumber}}
you can create your own
custom pipe filter
in which you can pass your number in the pipe and then, you can do code like below, put this logic in your custom pipe.Pipe filter
you can do like this.For creating custom pipe you can refer to this siteclick here