Hazem Labeeb
Hazem Labeeb

Reputation: 315

How to write superscript in a string and display using MessageBox.Show()?

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...

Upvotes: 21

Views: 44294

Answers (3)

Liam
Liam

Reputation: 400

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"; 

Upvotes: 0

Mobz
Mobz

Reputation: 374

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();

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149040

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.

Upvotes: 39

Related Questions