Formatting a Number to Have Commas At Every 1000 Factor

How to print a number using commas as thousands separators

Locale unaware

'{:,}'.format(value)  # For Python ≥2.7
f'{value:,}' # For Python ≥3.6

Locale aware

import locale
locale.setlocale(locale.LC_ALL, '') # Use '' for auto, or force e.g. to 'en_US.UTF-8'

'{:n}'.format(value) # For Python ≥2.7
f'{value:n}' # For Python ≥3.6


Per Format Specification Mini-Language,

The ',' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n' integer presentation type instead.

How to specify locale thousand separator for number pipe in Angular 4

Angular 5+

Since Angular 5, a locale argument has been added to the decimal pipe as you can see in the official documentation: That means you can choose your locale directly while calling the pipe, for instance:

{{ | number:'':'fr-FR'}}

Just be aware that will also change the decimal separator.

Angular 2+

or if your want to change ONLY the thousands separator...

According to Angular's documentation on DecimalPipe :, there is no explicit argument that can be added to the pipe call to exceptionally alter the characters used for formatting.

If you don't want to change the locale or associated default values of your entire project, I think your best shot is to write your own pipe dealing with your special case. Don't worry, pipes are extremely easy to write.

import { Pipe, PipeTransform } from '@angular/core';

name: 'numberfr'
export class FrenchDecimalPipe implements PipeTransform {

transform(val: number): string {
// Format the output to display any way you want here.
// For instance:
if (val !== undefined && val !== null) {
return val.toLocaleString(/*arguments you need*/);
} else {
return '';

Don't forget to add it to a NgModule to use it.

Angular: Formatting numbers with commas

Use DecimalPipe like this

{{attr | number}}

Working Plunker

Documentation available at

Format number in R with both comma thousands separator and specified decimals

format not formatC:

format(round(as.numeric(1000.64), 1), nsmall=1, big.mark=",") # 1,000.6

Add comma to numbers every three digits in datatable (R)

Had this same issue:

Try This:


df1 = data.frame(A=c(1000000.51,5000.33, 2500, 251), B=c(0.565,0.794, .685, .456))

df1 <- df1 %>% mutate(A=round(A,digits=0))

datatable(df1) %>% formatPercentage('B', 2) %>%
formatCurrency('A',currency = "", interval = 3, mark = ",")

JavaScript Sample Image 4

Comma Formatting for numbers in C++

Here is the brute force but may be easiest to understand way to get every thousand digits with the help of a vector.

#include <algorithm>
#include <vector>
using namespace std;
int main ( int argc, char * argv[] )
long long userInput;
int fthreeDigit;

cout << "Enter a long long number: " << endl;
cin >> userInput;
vector <int> res; //use vector to store every 3 digits
while (userInput !=0)
fthreeDigit = userInput %1000;
userInput = userInput / 1000 ;

std::reverse(res.begin(), res.end());
for (size_t i = 0; i < res.size()-1; ++i)
if (res[i] ==0)
cout << "000"<<",";
cout << res[i] << ",";

if (res[res.size()-1] == 0)
cout << "000";
cout << res[res.size()-1];
cout <<endl;
return 0;

I tested this code with the following case:

Input: 123456 Output: 123,456
Input: 12 Output: 12
Input: 12345 Output: 12,345
Input: 1234567 Output: 1,234,567
Input: 123456789 Output: 123,456,789
Input: 12345678 Output: 12,345,678

I guess this is what you want according to your response to comments.

How to Automatically add thousand separators as number is input in EditText

You can use String.format() in a TextWatcher. The comma in the format specifier does the trick.

This does not work for floating point input. And be careful not to set an infinite loop with the TextWatcher.

public void afterTextChanged(Editable view) {
String s = null;
try {
// The comma in the format specifier does the trick
s = String.format("%,d", Long.parseLong(view.toString()));
} catch (NumberFormatException e) {
// Set s back to the view after temporarily removing the text change listener

How to read data when some numbers contain commas as thousand separator?

I want to use R rather than pre-processing the data as it makes it easier when the data are revised. Following Shane's suggestion of using gsub, I think this is about as neat as I can do:

x <- read.csv("file.csv",header=TRUE,colClasses="character")
col2cvt <- 15:41
x[,col2cvt] <- lapply(x[,col2cvt],function(x){as.numeric(gsub(",", "", x))})

Related Topics

Leave a reply