e.g. My amount value is like (651156) and I want to display automatically like (6,51,156) using type script.
2 Answers
There is no such thing as TypeScript runtime functionality. A library could add a number format function but it will always be a JavaScript facility.
I am writing this because the framing of the question suggests a misunderstanding that may harm you later.
When you want to solve these problems, understand that TypeScript is a language that provides two things.
A. Tooling such as intellisense, static error checking, validation of API usage via types (manifest and inferred).
B. Transpilation of new or even prospective JavaScript features into older JavaScript syntax so you can use the latest JavaScript language features today.
It is not a runtime, it is a complementary set of tools that works with and enhances JavaScript for JavaScript.
So if you need to perform some computation, don't ask how to do it in TypeScript, ask how to do it in JavaScript, the answer will always be the same and you will get more responses.
Comments
In Angular 2 you would use pipes for that. https://angular.io/docs/ts/latest/guide/pipes.html Don't use the pure JS function that @ricky link shows, turn that into a pipe:
//imports
@Pipe({name: 'commaSeparatedNumber'})
export class CommaSeparatedNumberPipe implements PipeTransform {
transform(value:number) : string{
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
}
Add the commaSeparatedNumber to your declarations in the NgModule and you can use it like
{{ myNumber | commaSeparatedNumber }}
By the way, for decimals there would be a built-in pipe available in the CommonModule (which is also included in the FormsModule and BrowserModule): https://angular.io/docs/ts/latest/api/common/index/DecimalPipe-pipe.html
3 Comments
any when it returns string? Just leave the type off and the correct type will be inferred or, if you insist on being explicit, use string as the return type. Enough with the any already people! Also, the second parameter is wat!
jqueryorhtml, please don't add additional tags in the hope it will provide more attention. Trust the community!