Logo

Programming-Idioms

Create the end-user text, s, specifying the quantity, a, of unit x.

For example, "0 files", "1 file", or "1,000 files".
New implementation

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.

Other implementations
import java.text.ChoiceFormat;
String s = "0#files|1#file|1<files";
s = new ChoiceFormat(s).format(a);
s = "%,d %s".formatted(a, s);
const
  x: array[boolean] of string = ('file','files');
...
  s := format('%d %s',[a, x[Abs(a)>1]]));
x = ('no files', '1 file', f'{a:,} files')
s = x[bool(a)] if a < 2 else x[2]
s = f'{a:,} {('file', 'files')[a != 1]}'
s = "#{a} #{x}#{a == 1 ? "" : "s"}"