17

I am trying to output the area using a message box, and it should be displayed as, for example, 256 unit^2...

How can I write a superscript (for powers) and a subscript (like O2 for oxygen)???

This guy here adds a superscript like (TM):

Adding a TM superScript to a string

I Hope I got myself clear! Thanks in advance and sorry for any inconvenience...

Community
  • 1
  • 1
Hazem Labeeb
  • 265
  • 1
  • 3
  • 11

3 Answers3

33

You could try using unicode super/subscripts, for example:

var o2 = "O₂";       // or "O\x2082"
var unit2 = "unit²"; // or "unit\xB2"

If that doesn't work, I'm afraid you'll probably need to to write your own message box.

p.s.w.g
  • 141,205
  • 29
  • 278
  • 318
  • Hey, what about third power? I replaced '\xB2' with '\xB3' but it doesn't do the trick. – Jim Nov 13 '17 at 08:34
  • 2
    @Jim hmm, it works for me (tested on Windows 7 Pro). I wonder if the font on your system simply doesn't have a glyph for a superscript 3. – p.s.w.g Nov 13 '17 at 15:11
4

Here's superscripts and subscripts

wikipedia

And here's how to escape unicode characters in c#

MSDN

podiluska
  • 50,144
  • 7
  • 94
  • 100
Jonesopolis
  • 24,241
  • 10
  • 66
  • 110
  • Please modify this post to be complete in and of itself. Please extract the definitions from these links and in add them into this post so as to avoid [linkrot](http://en.wikipedia.org/wiki/Linkrot). A quote from help page [How do I write a good answer](https://stackoverflow.com/help/how-to-answer). "Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." – Alexander Ryan Baggett Feb 14 '19 at 17:29
0

I've used this extension for superscript.

    public static string ToSuperScript(this int number)
    {
        if (number == 0 ||
            number == 1)
            return "";

        const string SuperscriptDigits =
            "\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079";

        string Superscript = "";

        if (number < 0)
        {
            //Adds superscript minus
            Superscript = ((char)0x207B).ToString();
            number *= -1;
        }


        Superscript += new string(number.ToString()
                                        .Select(x => SuperscriptDigits[x - '0'])
                                        .ToArray()
                                  );

        return Superscript;
    }

Call it as

string SuperScript = 500.ToSuperScript();
Mobz
  • 304
  • 1
  • 12