1

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

4
  • 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 stackoverflow.com/a/2901298/4693938 Commented Feb 24, 2017 at 6:45
  • github.com/ExodusMovement/format-num/blob/master/README.md ... Here one demo for format number but I don't know how to use it. Commented Feb 24, 2017 at 6:55
  • what is your application environment..?? Commented Feb 24, 2017 at 7: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! Commented Feb 24, 2017 at 8:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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!
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.