Is it possible to write 10 ^ 2 in a special way?

Is it possible to write 10² or 10³ in C #?

For example, in a label or console output.

I also want to use it for other degrees (10 4 10 5 , ...).

Sort of:

 string specialNumber = string.Format("10^4"); System.Console.Write(specialNumber); 
+6
source share
5 answers

These are really two different questions. One for the console and one for the graphical interface. I prefer to close the console.

If you just need permissions 2 and 3, you can do this:

 Console.WriteLine("10²"); Console.WriteLine("10³"); 

This uses the characters U + 00B2 and U + 00B3 .

If it turns out that you need different permissions, you're out of luck on the console. Despite the fact that Unicode characters are for other numbers, font support leaves much to be desired, and you will not be able to execute such code:

 Console.WriteLine("10⁴"); Console.WriteLine("10⁵"); Console.WriteLine("10⁶"); // etc. 

Many commonly used console fonts do not contain superscript glyphs for these characters. For example, this looks like my car using Consolas:

enter image description here

If you use the default console font Lucinca Console, then the results will be the same.

+8
source

Here are superscripts and subscripts

wikipedia

But here's how to avoid unicode characters in C #

Msn

+3
source

If you want a complete solution (which can format a string based on the '^' character), you will have to minimize it yourself. Or ... you can use the one I just rolled up for you:

Function to replace the input character with the corresponding superscript character (note that this is an extension function ):

 public static char ToSuperscript( this char numericChar ) { switch ( numericChar ) { case '0': return '\u2070'; case '1': return '\u00B9'; case '2': return '\u00B2'; case '3': return '\u00B3'; case '4': return '\u2074'; case '5': return '\u2075'; case '6': return '\u2076'; case '7': return '\u2077'; case '8': return '\u2078'; case '9': return '\u2079'; default: return numericChar; } } 

Now I like to use LINQ, so I need a special extension method to handle the scan (thanks for MisterMetaphor for directing me to this function ):

 public static IEnumerable<U> Scan<T, U>( this IEnumerable<T> input, Func<U, T, U> next, U state ) { yield return state; foreach ( var item in input ) { state = next( state, item ); yield return state; } } 

Custom extension function using LINQ to apply superscript formatting:

 public static string FormatSuperscripts( this string unformattedString ) { return new string( unformattedString .ToCharArray() .Scan( ( state, currentChar ) => new { Char = state.IsSuperscript ? currentChar.ToSuperscript() : currentChar, IsSuperscript = ( currentChar >= '0' && currentChar <= '9' && state.IsSuperscript ) || currentChar == '^', IncludeInOutput = currentChar != '^' }, new { Char = ' ', IsSuperscript = false, IncludeInOutput = false } ) .Where( i => i.IncludeInOutput ) .Select( i => i.Char ) .ToArray() ); } 

And finally, the function call:

 string l_formattedString = "10^27 45^100".FormatSuperscripts(); 

Output:

10²⁷ 45¹⁰⁰

As already noted, the console does not display Unicode characters \ u2074 - \ u2079 correctly, but this function should work in scripts where the font supports these characters (for example, WPF, ASP.NET with a modern browser, etc.)

You can easily modify the above LINQ query to apply other formats, but I will leave this exercise to the readers.

+1
source

I don’t think there is automatic stuff, but you can write a conversion function:

 static char ToSuperscriptDigit(char input) { switch(input) { case '0': return '\u2070'; case '1': return '\u00B9'; case '2': return '\u00B2'; case '3': return '\u00B3'; case '4': return '\u2074'; case '5': return '\u2075'; case '6': return '\u2076'; case '7': return '\u2077'; case '8': return '\u2078'; case '9': return '\u2079'; default: return input; } } string ToSuperscriptDigits(string input) { if(input==null) return null; StringBuilder sb = new StringBuilder(input.Length); foreach(char c in input) sb.Append(ToSuperscriptDigit(c)); return sb.ToString(); } 

Please note: not all fonts display these characters in the same way: in Arial Unicode MS they all have the same style, but in the plain old Arial, Lucida Console, Courier New, etc. 1-3 are different from the rest. The Courier New 4-0 is not even monospaced!

0
source

If the question is how to enter the ² and ³ characters inside the text, just enter them. This has nothing to do with C #, it is related to 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 does not work this way, you can use the built-in utility in Windows to find shortcuts used to enter special characters. ² - Alt + 0178 in the numeric keypad, ³ - Alt + 0179

Some keyboards even mark the right Alt as “Alt GR” to show it for entering “Graphics” characters.

All special characters were entered using the methods described.

0
source

Source: https://habr.com/ru/post/948221/


All Articles