How to Format a Number 1000 as "1 000"

How to format a number 1000 as 1 000

see: http://www.justskins.com/forums/format-number-with-comma-37369.html

there is no built in way to it ( unless you using Rails, ActiveSupport Does have methods to do this) but you can use a Regex like

formatted_n = n.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse

Format a number 1000 as 1k, 1000000 as 1m etc. in R

Using dplyr::case_when:

so_formatter <- function(x) {
dplyr::case_when(
x < 1e3 ~ as.character(x),
x < 1e6 ~ paste0(as.character(x/1e3), "K"),
x < 1e9 ~ paste0(as.character(x/1e6), "M"),
TRUE ~ "To be implemented..."
)
}

test <- c(1, 999, 1000, 999000, 1000000, 1500000, 1000000000, 100000000000)
so_formatter(test)


# [1] "1"
# [2] "999"
# [3] "1K"
# [4] "999K"
# [5] "1M"
# [6] "1.5M"
# [7] "To be implemented..."
# [8] "To be implemented..."

How to format clean numbers so 1000 appear as 1.000,00

If you want a simple function to do the job, the following may suit:

// Format a number n using: 
// p decimal places (two by default)
// ts as the thousands separator (comma by default) and
// dp as the decimal point (period by default).
//
// If p < 0 or p > 20 results are implementation dependent.
function formatNumber(n, p, ts, dp) {
var t = [];
// Get arguments, set defaults
if (typeof p == 'undefined') p = 2;
if (typeof ts == 'undefined') ts = ',';
if (typeof dp == 'undefined') dp = '.';

// Get number and decimal part of n
n = Number(n).toFixed(p).split('.');

// Add thousands separator and decimal point (if requied):
for (var iLen = n[0].length, i = iLen? iLen % 3 || 3 : 0, j = 0; i <= iLen; i+=3) {
t.push(n[0].substring(j, i));
j = i;
}
// Insert separators and return result
return t.join(ts) + (n[1]? dp + n[1] : '');
}


//*
console.log(formatNumber(
1234567890.567, // value to format
4, // number of decimal places
'.', // thousands separator
',' // decimal separator
)); // result: 1.234.567.890,5670
//*/

console.log(formatNumber(
123.567, // value to format
1 // number of decimal places
)); // result: 123.6

console.log(formatNumber(
'123.567', // value to format
0 // number of decimal places
)); // result: 123.6

console.log(formatNumber(
123, // value to format
0 // number of decimal places
)); // result: 123

console.log(formatNumber(
13, // value to format
2 // number of decimal places
)); // result: 13.00

console.log(formatNumber(
0 // value to format
// number of decimal places
)); // result: 0.00

console.log(formatNumber(
// value to format
// number of decimal places
)); // result: NaN

Sorry, no fancy regular expressions or slice/splice array stuff, just POJS that works.

How to print a number with commas as thousands separators in JavaScript

I used the idea from Kerry's answer, but simplified it since I was just looking for something simple for my specific purpose. Here is what I have:

function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}





function numberWithCommas(x) {
return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
}

function test(x, expect) {
const result = numberWithCommas(x);
const pass = result === expect;
console.log(`${pass ? "✓" : "ERROR ====>"} ${x} => ${result}`);
return pass;
}

let failures = 0;
failures += !test(0, "0");
failures += !test(100, "100");
failures += !test(1000, "1,000");
failures += !test(10000, "10,000");
failures += !test(100000, "100,000");
failures += !test(1000000, "1,000,000");
failures += !test(10000000, "10,000,000");
if (failures) {
console.log(`${failures} test(s) failed`);
} else {
console.log("All tests passed");
}
.as-console-wrapper {
max-height: 100% !important;
}

How to format numbers as thousands separators in Dart

You can use NumberFormat passing a custom format in ICU formatting pattern, take a look in NumberFormat.

import 'package:intl/intl.dart';

void main() {
var formatter = NumberFormat('#,##,000');
print(formatter.format(16987));
print(formatter.format(13876));
print(formatter.format(456786));
}

Output

16,987
13,876
4,56,786

How to set thousands separator in Java?

This should work (untested, based on JavaDoc):

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(' ');
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(bd.longValue()));

According to the JavaDoc, the cast in the first line should be save for most locales.

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

Reference

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.

Format a number as 2.5K if a thousand or more, otherwise 900

Sounds like this should work for you: