Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #358 Format a quantity in unit notation

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

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

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"}"

New implementation...
< >
reilas