To allow for an item-count display to handle a number that may or may not be one, this display usually includes something like item(s), file(s), folder(s), document(s), second(s), minute(s), hour(s), or day(s). In some programs, the parentheses around the "
s" are left out. In those cases, there is an apparent assumption that the quantity will rarely be one. It would be better to display an "
s" only when needed, without parentheses.
The following solution only works for English text. There are various articles on CodeProject that address this problem for other languages.
To make it easy to display an "
s" only when needed, I wrote the following macro:
#define SINGULAR(n) (n == 1 ? _T("") : _T("s"))
If you only want to display Unicode strings, replace
To use it, put this macro into an
include file that is included by source files that need it. Then, write code such as the following:
strMsg.Format (_T("%d item%s"), n, SINGULAR(n));
strMsg. For words such as "
index" whose plural form ends in "
es", use the following macro:
#define SINGULAR_ES(n) (n == 1 ? _T("") : _T("es"))
For words such as "
man" whose plural form does not fit either of these patterns, use an inline conditional:
strMsg.Format (_T("%d %s"), n, (n==1 ? _T("man") : _T("men")));
Singular demonstrates these three circumstances for selecting between singular and plural forms of words.
While working as an engineer, I occasionally applied my problem-solving skills to software problems. For example, I noticed that numbers in increasing filenames did not always increase. I tinkered with solutions and subsequently wrote an article that was published in Dr. Dobb’s Journal.
After that job fizzled, I switched to programming full time and have applied my programming skills to multimedia education, web-based games of skill, legal-compliance and ethics-training courses, a portable PET scanner, and shareware programs.