#Is possible print e.g. "X²" instead of "X^2" but the exponent as a variable?

10 messages · Page 1 of 1 (latest)

gusty spade
#

In Java using Intellij IDEA is possible to print a variable like this "X²" (being "2" the variable) instead of "X^2"?

plucky steepleBOT
#

This post has been reserved for your question.

Hey @gusty spade! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

limber wyvern
#

What?

sturdy forge
#

You can use unicode
0: ⁰ - U+2070
1: ¹ - U+00B9
2: ² - U+00B2
3: ³ - U+00B3
4: ⁴ - U+2074
5: ⁵ - U+2075
6: ⁶ - U+2076
7: ⁷ - U+2077
8: ⁸ - U+2078
9: ⁹ - U+2079

#
String number0 = "\u2070";
System.out.println("Number : " + number0);
Number : ⁰
#

and for the variable use something like this

#
public static String toSuperscript(int number) {
        char[] superscriptDigits = {'\u2070', '\u00B9', '\u00B2', '\u00B3', '\u2074', '\u2075', '\u2076', '\u2077', '\u2078', '\u2079'};
        
        String numberStr = Integer.toString(number);
        StringBuilder superscriptStr = new StringBuilder();

        for (char digit : numberStr.toCharArray()) {
            int digitValue = Character.getNumericValue(digit);
            superscriptStr.append(superscriptDigits[digitValue]);
        }

        return superscriptStr.toString();
    }
plucky steepleBOT
# gusty spade Ok, thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.