19

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
  • 295
  • 1
  • 3
  • 11

3 Answers3

37

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
  • 146,324
  • 30
  • 291
  • 331
  • 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
  • Here's superscripts and subscripts [wikipedia](https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts) And here's how to escape unicode characters in c# [MSDN](http://msdn.microsoft.com/en-us/library/aa664669%28v=vs.71%29.aspx) – Jonesopolis Jul 17 '13 at 15:39
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
  • 344
  • 1
  • 16
0

I've been using html string formatting which c# in Unity seems to decode nicely, and adds more flexibility then the limited unicode subscripts and superscripts options, i.e:

string To256PowerOf2String = "256<sup>2</sup>";
string H2OString = "H<sub>2</sub>O"; 
Liam
  • 390
  • 5
  • 11