Function to get Unicode Fractions

Murray Sargent

Do you know that Unicode includes the fraction characters ↉ ½ ⅓ ¼ ⅕ ⅙ ⅐ ⅛ ⅑ ⅒ ⅔ ⅖ ¾ ⅗ ⅜ ⅘ ⅚ ⅞? Well thanks to existing character standards, ½ ⅓ ¼ ⅕ ⅙ ⅛ ⅔ ⅖ ¾ ⅗ ⅜ ⅘ ⅚ ⅞ were added in Unicode 1.1 in 1993, and ↉ ⅐ ⅑ ⅒ were added in Unicode 5.2 in 2009. Programs like Microsoft Word have an “Autoformat as you type” option to convert the linear fractions 1/2, 1/3, 1/4, and 3/4 into the corresponding Unicode fraction ½ ⅓ ¼ and ¾. This post gives a simple C++ function that converts the linear form of all Unicode fractions into the Unicode fraction characters. The function is relatively easy to read and understand, at least if you know some programming, since it uses the Unicode fractions themselves instead of hard-to-recognize numeric references like \x2153 (2153 is the hexadecimal code for ⅓). The function runs on Windows, Mac, iOS, Android, and likely other platforms.

wchar_t GetUnicodeFraction(‎
‎    wchar_t chNum,‎				‎// Numerator character
‎    wchar_t chDenom) noexcept	                ‎// Denominator character
‎{‎
‎    static const wchar_t rgchNum1[] = L"½⅓¼⅕⅙⅐⅛⅑⅒";‎

‎    switch (chNum)‎
‎    {‎
‎        case '0':‎
‎            return chDenom == '3' ? L'↉' : 0;‎	‎// Used in baseball scoring‎

‎        case '1':‎			        ‎// ':' (0x003A) is used for '10'‎
‎            return IN_RANGE('2', chDenom, ':') ? rgchNum1[chDenom - '2'] : 0;‎

‎        case '2':‎
‎            return chDenom == '3' ? L'⅔' : chDenom == '5' ? L'⅖' : 0;‎

‎        case '3':‎
‎            return chDenom == '4' ? L'¾' : chDenom == '5' ? L'⅗'‎
‎                 : chDenom == '8' ? L'⅜' : 0;‎

‎        case '4':‎
‎            return chDenom == '5' ? L'⅘' : 0;‎

‎        case '5':‎
‎            return chDenom == '6' ? L'⅚' : chDenom == '8' ? L'⅝' : 0;‎

‎        case '7':‎
‎            return chDenom == '8' ? L'⅞' : 0;‎
‎    }‎
‎    return 0;‎
‎}‎

Here the IN_RANGE(‘2’, chDenom, ‘:’) macro is true for all characters in the range ‘2’ through ‘:’, that is 23456789:, and the ‘:’ is used for the 10 in ‎⅒‎.

Occasionally, people recommend limiting programs to the ASCII characters in Unicode since some programming tools can’t handle other Unicode characters. But ASCII was invented in 1963 and contemporary programming tools should be able to handle Unicode, which has been the de facto character standard for many years. I use Unicode math and braille characters extensively in the RichEdit math editing and display code, and the code is much easier to read and maintain than when ASCII numeric references are used.

Inside math zones, you can enter arbitrary built-up fractions, but it can be handy to have the Unicode fractions in ordinary text. As you might guess, the GetUnicodeFraction‎() function is used in the new RichEdit “Autoformat as you type” option, an option that will be discussed in a future post.