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?
Asked
Active
Viewed 1.6k times
6
-
2How? Print to console, or some form of UI, or output in a doc, or what? Java supports unicode. Have you tried just `System.out.println("1900 km²");`? – tobias_k Aug 27 '15 at 14:59
-
Do you mean `^` or you want superscript? – Ivaylo Strandjev Aug 27 '15 at 15:00
-
Possible duplicate. http://stackoverflow.com/questions/456002/displaying-fancy-equations-with-java – Januson Aug 27 '15 at 15:00
-
1https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts – Andy Turner Aug 27 '15 at 15:01
-
My keyboard has a Shortcut for it: AltGr+2 makes a ². Other than that, Java files upport Unicode. Just get the character somewhere from the Internet and copy/paste into your source file. – Thomas Weller Aug 27 '15 at 15:02
-
possible duplicate of [How to insert a special character?](http://stackoverflow.com/questions/15596164/how-to-insert-a-special-character) – Thomas Weller Aug 27 '15 at 15:04
-
The console only has one font, unlike HTML. This means if you want super script you have to use different characters. http://www.fileformat.info/info/unicode/char/b2/index.htm – Peter Lawrey Aug 27 '15 at 15:17
4 Answers
4
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

Davide Lorenzo MARINO
- 26,420
- 4
- 39
- 56
1
You can try to use the "SUPERSCRIPT TWO" Unicode Character (escape code \u00B2) if your font supports it.

Cristian Veronesi
- 300
- 1
- 3
- 15
1
You can use the string literal "km\u00B2"
, or just use km²
directly in your source code if you are using a unicode-supporting file encoding.

Andy Turner
- 137,514
- 11
- 162
- 243