开发者

Code to calculate the unicode character for a fraction in C# .NET

I'm looking for a way in C# .NET to get t开发者_运维百科he Unicode character corresponding to a given fraction. In my code I have the numerator and denominator and I need to work out how to get the corresponding Unicode character from these, which is then displayed in a WinForms combobox.

Any ideas?


You will need a lookup table, there is no algorithmic method available (Unicode codepoints are not allocated like that).

Of course most fractions do not have a codepoint, only a few "common" fractions have been added.

And of course, even with the codepoint your typeface may not include them.


public static string VulgarFraction(int numerator, int denominator)
{
  if(denominator == 0)
    return "\u221E";
  if(denominator < 0)
    return VulgarFraction(-numerator, -denominator);
  return numerator.ToString() + '\u2044' + denominator.ToString();
}

The precomposed vulgar fractions are included for compatibility, and not encouraged. Use U+2044 FRACTION SLASH to indicate a fraction. This indicates the desire to render as a fraction to a layout system, and is also widely supported by less sophisticated rendering methods.

If you really do need to use the precomposed characters, then a look-up table is the only reasonable way (you could build a big set of branching ifs and elses, but it's more work for less performance).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜