Logo

Programming-Idioms

  • Fortran
  • Rust
  • Ruby

Idiom #173 Format a number with grouped thousands

Number will be formatted with a comma separator between every group of thousands.

'1000'.reverse.scan(/.{1,3}/).join(',').reverse
'1000'.gsub(/\B(?=(...)*\b)/, ',')
require 'active_support/all'
1000.to_s(:delimited)

requires packages ruby-activesupport, ruby-bigdecimal and ruby-json
use separator::Separatable;
println!("{}", 1000.separated_string());

Requires the separator crate
#define _POSIX_C_SOURCE 200809L
#include <locale.h>
#include <stdio.h>
setlocale(LC_ALL, "");
printf("%'d\n", 1000);

New implementation...
cup