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

3

20

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

Doherty answered 17/7, 2013 at 15:36 Comment(0)
P
39

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.

Pork answered 17/7, 2013 at 15:38 Comment(3)
Here's superscripts and subscripts wikipedia And here's how to escape unicode characters in c# MSDNKakapo
Hey, what about third power? I replaced '\xB2' with '\xB3' but it doesn't do the trick.Badman
@Badman 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.Pork
T
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();
Testudo answered 14/2, 2022 at 13:31 Comment(0)
F
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"; 
Fortnight answered 27/1, 2023 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.