Function to get Unicode Fractions
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.
5 comments
Do you know what the purpose of ↉ is?
It doesn’t seem terribly useful. Seems like it must have some special purpose.
Thanks
These characters are a trap. They make no end of trouble for software developers who implement machine readability. Besides, they aren’t kind on human eyes either. Finally, the software engineering documents containing them quickly become inconsistent. Try writing: 1/2, 1/4, 1/8, 1/16, 1/32, 1/64, 1/128, and 1/256.
Technically, it is possible to develop a font that renders digits around the U+2044 FRACTION SLASH or U+2215 DIVISION SLASH into correct positions. Doing so fixes the machine readability problem but does nothing about the human readability problem.
I think the Unicode fractions are only intended for simple ordinary text. As soon as text gets more technical, insert a math zone (e.g., Alt+= in Word) and enter an arbitrary fraction by typing the numerator digits, ‘/’, denominator digits, ‘ ‘. Or the LaTeX \frac{}{}
You can also get arbitrary numeric fractions using the Unicode superscript and subscript numbers along with the U+2044 fraction slash. For example, ⁴⁵⁶⁄₇₈₉₀ Admittedly it’s not great typography, but it works. To enter such characters, it’s handy to have the Alt+x hot key 🙂 In Fleet’s comment above, you can enter ½ ¼, ⅛, ⅟₁₆, ⅟₃₂, ⅟₆₄, ⅟₁₂₈, and ⅟₂₅₆, where I use ⅟ (U+215F) for the numerator and fraction slash.
this comment has been deleted.