Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a number to have commas at every 1000 factor

I need to format a number like 1234567 as 1,234,567 but don't know how to do this. I tried using currency pipe of TypeScript but that gives USD or $ in front of the number. I want to remove that and format the number in this way 1,234,567. How can I do this?

like image 591
suman Avatar asked Nov 17 '16 17:11

suman


People also ask

How do you format a thousands separator?

The character used as the thousands separatorIn the United States, this character is a comma (,). In Germany, it is a period (.). Thus one thousand and twenty-five is displayed as 1,025 in the United States and 1.025 in Germany. In Sweden, the thousands separator is a space.

How do you add commas to thousands separator for numbers in Python?

In Python, to format a number with commas we will use “{:,}” along with the format() function and it will add a comma to every thousand places starting from left.


2 Answers

Just use the number (decimal) pipe instead.

To give an example:

{{ '1234567' | number:'1.0':'en-US' }} 

will produce output 1,234,567.

If you do not change the default locale (by calling registerLocaleData() or providing LOCALE_ID), then simple {{'1234567' | number}} will work as well.

like image 118
Daniel Kucal Avatar answered Oct 13 '22 16:10

Daniel Kucal


The last answer will produce 1,234,567.00

Note the 2 decimals.

Use the following Pipe:

{{ element.total | number: '2.'}}

In order to produce 1,234,567

like image 32
Itamar Avatar answered Oct 13 '22 16:10

Itamar