{"id":387,"date":"2022-01-22T15:46:24","date_gmt":"2022-01-22T23:46:24","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/math-in-office\/?p=387"},"modified":"2022-01-22T15:46:24","modified_gmt":"2022-01-22T23:46:24","slug":"function-to-get-unicode-fractions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/math-in-office\/function-to-get-unicode-fractions\/","title":{"rendered":"Function to get Unicode Fractions"},"content":{"rendered":"<p>Do you know that Unicode includes the fraction characters \u2189 \u00bd \u2153 \u00bc \u2155 \u2159 \u2150 \u215b \u2151 \u2152 \u2154 \u2156 \u00be \u2157 \u215c \u2158 \u215a \u215e? Well thanks to existing character standards, \u00bd \u2153 \u00bc \u2155 \u2159 \u215b \u2154 \u2156 \u00be \u2157 \u215c \u2158 \u215a \u215e were added in Unicode 1.1 in 1993, and \u2189 \u2150 \u2151 \u2152 were added in Unicode 5.2 in 2009. Programs like Microsoft Word have an \u201cAutoformat as you type\u201d option to convert the linear fractions 1\/2, 1\/3, 1\/4, and 3\/4 into the corresponding Unicode fraction \u00bd \u2153 \u00bc and \u00be. 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 \u2153). The function runs on Windows, Mac, iOS, Android, and likely other platforms.<\/p>\n<pre>wchar_t GetUnicodeFraction(\u200e\r\n\u200e    wchar_t chNum,\u200e\t\t\t\t\u200e\/\/ Numerator character\r\n\u200e    wchar_t chDenom) noexcept\t                \u200e\/\/ Denominator character\r\n\u200e{\u200e\r\n\u200e    static const wchar_t rgchNum1[] = L\"\u00bd\u2153\u00bc\u2155\u2159\u2150\u215b\u2151\u2152\";\u200e\r\n\r\n\u200e    switch (chNum)\u200e\r\n\u200e    {\u200e\r\n\u200e        case '0':\u200e\r\n\u200e            return chDenom == '3' ? L'\u2189' : 0;\u200e\t\u200e\/\/ Used in baseball scoring\u200e\r\n\r\n\u200e        case '1':\u200e\t\t\t        \u200e\/\/ ':' (0x003A) is used for '10'\u200e\r\n\u200e            return IN_RANGE('2', chDenom, ':') ? rgchNum1[chDenom - '2'] : 0;\u200e\r\n\r\n\u200e        case '2':\u200e\r\n\u200e            return chDenom == '3' ? L'\u2154' : chDenom == '5' ? L'\u2156' : 0;\u200e\r\n\r\n\u200e        case '3':\u200e\r\n\u200e            return chDenom == '4' ? L'\u00be' : chDenom == '5' ? L'\u2157'\u200e\r\n\u200e                 : chDenom == '8' ? L'\u215c' : 0;\u200e\r\n\r\n\u200e        case '4':\u200e\r\n\u200e            return chDenom == '5' ? L'\u2158' : 0;\u200e\r\n\r\n\u200e        case '5':\u200e\r\n\u200e            return chDenom == '6' ? L'\u215a' : chDenom == '8' ? L'\u215d' : 0;\u200e\r\n\r\n\u200e        case '7':\u200e\r\n\u200e            return chDenom == '8' ? L'\u215e' : 0;\u200e\r\n\u200e    }\u200e\r\n\u200e    return 0;\u200e\r\n\u200e}\u200e\r\n\r\n<\/pre>\n<p>Here the IN_RANGE(&#8216;2&#8217;, chDenom, &#8216;:&#8217;) macro is true for all characters in the range \u20182\u2019 through \u2018:\u2019, that is 23456789:, and the \u2018:\u2019 is used for the 10 in \u200e\u2152\u200e.<\/p>\n<p>Occasionally, people recommend limiting programs to the ASCII characters in Unicode since some programming tools can\u2019t 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.<\/p>\n<p>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\u200e() function is used in the new RichEdit \u201cAutoformat as you type\u201d option, an option that will be discussed in a future post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you know that Unicode includes the fraction characters \u2189 \u00bd \u2153 \u00bc \u2155 \u2159 \u2150 \u215b \u2151 \u2152 \u2154 \u2156 \u00be \u2157 \u215c \u2158 \u215a \u215e? Well thanks to existing character standards, \u00bd \u2153 \u00bc \u2155 \u2159 \u215b \u2154 \u2156 \u00be \u2157 \u215c \u2158 \u215a \u215e were added in Unicode 1.1 in 1993, and [&hellip;]<\/p>\n","protected":false},"author":40611,"featured_media":55,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-387","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-math-in-office"],"acf":[],"blog_post_summary":"<p>Do you know that Unicode includes the fraction characters \u2189 \u00bd \u2153 \u00bc \u2155 \u2159 \u2150 \u215b \u2151 \u2152 \u2154 \u2156 \u00be \u2157 \u215c \u2158 \u215a \u215e? Well thanks to existing character standards, \u00bd \u2153 \u00bc \u2155 \u2159 \u215b \u2154 \u2156 \u00be \u2157 \u215c \u2158 \u215a \u215e were added in Unicode 1.1 in 1993, and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/math-in-office\/wp-json\/wp\/v2\/posts\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/math-in-office\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/math-in-office\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/math-in-office\/wp-json\/wp\/v2\/users\/40611"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/math-in-office\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/math-in-office\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/math-in-office\/wp-json\/wp\/v2\/media\/55"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/math-in-office\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/math-in-office\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/math-in-office\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}