Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- C
- C#
- D
- Go
- Haskell
- JS
- Java
- Java
- Java
- Java
- Java
- Java
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Ruby
- Ruby
- Ruby
- Rust
- Scala
setlocale(LC_ALL, "");
printf("%'d\n", 1000);
$"{1000:n}"
s :: Int -> String
s = intersperseN ',' 3 . show
intersperseN :: a -> Int -> [a] -> [a]
intersperseN x n = uncurry (<>) . foldr alg ([], [])
where
alg a (buf', acc)
| length buf >= n = ([], (x:buf) <> acc)
| otherwise = (buf, acc)
where buf = a:buf'
new Intl.NumberFormat().format(1000);
out.printf("%,d", new BigInteger("1234"));
DecimalFormat f = (DecimalFormat) getNumberInstance();
f.setGroupingSize(3);
String s = f.format(1_000);
NumberFormat f = new DecimalFormat("#,###");
String s = f.format(1_234);
Note, this will truncate any fractional portion—e.g., "1234.5" to "1,234".
String.format("%,d", 1000000);
out.printf("%,f", new BigDecimal("1234.5"));
echo number_format(1000);
sub commify {
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}
Loop over a string and insert a string every three characters, until you can't.
'{:,}'.format(1000)
f'{1000:,}'
format(1000, ',')
'1000'.reverse.scan(/.{1,3}/).join(',').reverse
1000.to_s(:delimited)
requires packages ruby-activesupport, ruby-bigdecimal and ruby-json
'1000'.gsub(/\B(?=(...)*\b)/, ',')
"%,d".format(n)