Is it possible to write 10²
or 10³
in C#?
For example in a label or Console output.
I also want use it for other powers (104, 105, ...).
Something like:
string specialNumber = string.Format("10^4");
System.Console.Write(specialNumber);
Is it possible to write 10²
or 10³
in C#?
For example in a label or Console output.
I also want use it for other powers (104, 105, ...).
Something like:
string specialNumber = string.Format("10^4");
System.Console.Write(specialNumber);
If you'd like a complete solution (which is able to format the string based on the
'^'
character), then you'll have to roll your own. Or... you can use the one I just rolled up for you:The function to replace an input character with the corresponding superscript character (notice that it is an extension function):
Now, I like to use LINQ, so I need a custom extension method to handle the scan (thanks go the MisterMetaphor for directing me to this function):
A custom extension function using LINQ to apply the superscript formatting:
And, finally, calling the function:
Output:
As already noted, however, the console will not correctly display unicode characters \u2074 - \u2079, but this function should work in scenarios where the font supports these characters (such as WPF, ASP.NET with a modern browser, etc)
You could easily modify the above LINQ query to apply other formattings as well, but I will leave that exercise to the readers.
I don't think there is an automatic stuff, but you can write a conversion function:
Note that not all fonts render these characters equally: In Arial Unicode MS they all have the same style, but in plain old Arial, Lucida Console, Courier New etc., 1-3 are different from the rest. In Courier New, 4-0 aren't even monospaced!
This is really two different questions. One for the console, and one for a GUI app. I'm choosing to cover the console.
If you just need powers 2 and 3 you can do this:
This makes use of characters U+00B2 and U+00B3.
If it turns out that you require different powers then you are out of luck at the console. Whilst there are Unicode characters for other numerals, font support is poor and you will have no success with code like this:
Many commonly used console fonts do not include superscript glyphs for these characters. For example, this is what it looks like on my machine using Consolas:
If you are using the default console font of Lucinda Console, then the results are the same.
Here's superscripts and subscripts
wikipedia
And here's how to escape unicode characters in c#
MSN
If the question is how to enter the ² and ³ characters inside a text - just type them. It has nothing to do with C#, it has to do with your keyboard. In my case (Greek keyboard layout), I pressed Right Alt + Right Ctrl + 2 for ² or Right Alt + Right Ctrl + 3 for ³.
If your layout doesn't work this way, you can use the Character Map built-in utility in Windows to find the shortcuts used for entering special characters. ² is Alt+0178 in the numeric keypad, ³ is Alt+0179
Some keyboards even mark the Right Alt as "Alt GR" to show it's for entering "Graphics" characters.
All special characters were entered using the methods described.