1

e.g. My amount value is like (651156) and I want to display automatically like (6,51,156) using type script.

bergben
  • 1,385
  • 1
  • 16
  • 35
  • 3
    There is no inbuilt or predefined method in java script or type script for this. You can create your function for this. see here http://stackoverflow.com/a/2901298/4693938 – ricky Feb 24 '17 at 06:45
  • https://github.com/ExodusMovement/format-num/blob/master/README.md ... Here one demo for format number but I don't know how to use it. – smartsense-designer Feb 24 '17 at 06:55
  • what is your application environment..?? – Vivek Singh Feb 24 '17 at 07:00
  • 1
    This question doesn't have anything to do with `jquery` or `html`, please don't add additional tags in the hope it will provide more attention. Trust the community! – Christopher Moore Feb 24 '17 at 08:40

2 Answers2

2

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.

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
1

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

bergben
  • 1,385
  • 1
  • 16
  • 35
  • You have some syntax errors. Also, why are you saying that the pipe returns `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!_ – Aluan Haddad Feb 24 '17 at 10:20
  • Maybe you should try to calm down a bit. It was a sample code created in a haste with mostly copy and paste for the sole purpose to make the asker understand that he has to use pipes. Nobody goes crazy about your answer not answering the actual question so maybe it would be adequate to rage less. – bergben Feb 24 '17 at 10:32
  • 1
    Relax I didn't downvote or anything. I was intending to be a bit tongue-in-cheek about the any type. But frankly I see this problem all the time, people adding the any type annotation, reducing their code quality while increasing the work they have to do and defeating the point the language. It frustrates me. Your technique for formatting the number is quite nice. – Aluan Haddad Feb 24 '17 at 10:37