Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java how to insert a "power of" symbol?

Tags:

java

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?

like image 264
user3929251 Avatar asked Aug 27 '15 14:08

user3929251


People also ask

How do you put something to the power of in Java?

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.

How do you write 10 to the power 9 in Java?

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.

How do you add symbols in Java?

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.


3 Answers

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

like image 110
Scott Avatar answered Sep 19 '22 17:09

Scott


If your output is an html page:

km<sup>2</sup> 

or

km&sup2; 

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

like image 45
Davide Lorenzo MARINO Avatar answered Sep 17 '22 17:09

Davide Lorenzo MARINO


You can try to use the "SUPERSCRIPT TWO" Unicode Character (escape code \u00B2) if your font supports it.

like image 30
Cristian Veronesi Avatar answered Sep 18 '22 17:09

Cristian Veronesi