plurality

Selects the correct singular or plural form of a word depending on the numerical count of it.

Technically works with any type provided the number is some comparable integral.

@safe pragma(inline, true) pure nothrow @nogc
T
plurality
(
Num
T
)
(
const Num num
,
const return scope T singular
,
const return scope T plural
)

Parameters

num Num

Numerical count.

singular T

The singular form.

plural T

The plural form.

Return Value

Type: T

The singular if num is 1 or -1, otherwise the plural.

Examples

string one = 1.plurality("one", "two");
string two = 2.plurality("one", "two");
string many = (-2).plurality("one", "many");
string many0 = 0.plurality("one", "many");

assert((one == "one"), one);
assert((two == "two"), two);
assert((many == "many"), many);
assert((many0 == "many"), many0);
static assert(10.plurality("one","many") == "many");
static assert(1.plurality("one", "many") == "one");
static assert((-1).plurality("one", "many") == "one");
static assert(0.plurality("one", "many") == "many");