I am unable to print numbers in superscript when the string contains variables.
This works:
Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.Write("2\xB3");
This does not:
Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.WriteLine($"\n{expressionBase}\xB{expressionExponent} = {result}");
Any help would be greatly appreciated.
I've searched around and can't find an example where this works w/anything other than a string of plain text w/out string interpolation being used.
I am unable to print numbers in superscript when the string contains variables.
This works:
Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.Write("2\xB3");
This does not:
Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.WriteLine($"\n{expressionBase}\xB{expressionExponent} = {result}");
Any help would be greatly appreciated.
I've searched around and can't find an example where this works w/anything other than a string of plain text w/out string interpolation being used.
You seem to assume that the \xB0
is superscript 0, \xB1
is superscript 1, and \xB9
is superscript 9, and so on. This is not true. The mapping from each digit to each superscript character is like this:
char SuperscriptDigit(int digit) => digit switch {
1 => '\xB9',
2 => '\xB2',
3 => '\xB3',
0 or (> 3 and < 10) => (char)('\x2070' + digit),
_ => throw new ArgumentException()
};
That is, only 0, and 3 to 9 follows a consistent pattern. \x2075
is superscript 5, \x2079
is superscript 9, etc.
Then you can write a function that gets all the digits from a number, and passes them all to SuperscriptDigit
, and finally concatenates all of them together to form a string.
string SuperscriptString(int n) {
if (n == 0) return "\x2070";
var builder = new StringBuilder();
var negative = n < 0;
for(; n != 0; n /= 10) {
int digit = Math.Abs(n % 10);
builder.Insert(0, SuperscriptDigit(digit));
}
if (negative) {
builder.Insert(0, '\x207B'); // U+207B is the superscript negative sign
}
return builder.ToString();
}
Finally, you can do
int baseNumber = 54321;
int exponentNumber = 12345;
Console.Write($"{baseNumber}{SuperscriptString(exponentNumber)}");
// prints 54321¹²³⁴⁵
\xB3
encodes a single character (³
) while \xB{expressionExponent}
part of the interpolated string encodes a character (\xB
) + basically a string ({expressionExponent}
).
You can use mapping to get needed character:
Dictionary<char, char> mapping = new()
{
['1'] = '¹',
['2'] = '²',
['3'] = '³',
['4'] = '⁴',
['5'] = '⁵',
['6'] = '⁶',
['7'] = '⁷',
['8'] = '⁸',
['9'] = '⁹',
['0'] = '⁰',
};
Console.OutputEncoding = System.Text.Encoding.Unicode;
var expressionBase = '2';
var expressionExponent = '3';
var result = 8;
Console.WriteLine($"\n{expressionBase}{mapping[expressionExponent]} = {result}");
Or in case of just numbers:
var mapping = new [] {'⁰','¹','²','³','⁴','⁵','⁶','⁷','⁸','⁹'};
// ...
var expressionExponent = 3;
Console.WriteLine($"\n{expressionBase}{mapping[expressionExponent]} = {result}");
Note that you will need to map every single digit of the number (of the expressionExponent
).