Hello I have a program that displays the area of some land. The number is 1900 square kilometers. I want to write this as 1900 km2 but the two should look like a "to the power of 2" symbol. Is there a way I can insert a symbol like that?
Math. pow(double a, double b) returns the value of a raised to the power of b . It's a static method on Math class, which means you don't have to instantiate a Math instance to call it. The power of a number is the number of times the number is multiplied by itself.
pow() to take a large exponent. Since 109 fits into an int and is also exactly representable as a double , you can do this: int exp = (int) Math. pow(10, 9); BigInteger answer = BigInteger.
You can create a Character object with the Character constructor: Character ch = new Character('a'); The Java compiler will also create a Character object for you under some circumstances.
Unicode code u00B2
will give you the superscript two symbol. Try the following:
System.out.println("km\u00B2");
Working example
You could alternatively use the extended ASCII code alt + 253
as stated here
If your output is an html page:
km<sup>2</sup>
or
km²
If your output is a String to the console
System.out.println("km\u00B2");
Other outputs
If you need to print it to other systems (pdf, excel...) and they accept unicode values use the char '\u00B2'
for the 2 at the exponent
You can try to use the "SUPERSCRIPT TWO" Unicode Character (escape code \u00B2) if your font supports it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With