Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

How to output a number raised to the power in Java

Hello, I'm finishing up this application I am writing that displaced the cubic feet of boxes. However it's just printing 355ft. I need it to print 355ft^3 BUT NOT THE "^" SYMBOL. The three needs to be smaller too, since it's cubic feet. Any ideas?

Thanks,

3 Answers

Michael Davis
PLUS
Michael Davis
Courses Plus Student 12,508 Points

It sounds like, to me, that you are trying to get the 3, representing "cubed", to show as superscript. Displaying text in the superscript or subscript is a formatting task, and as such would require some sort of text manipulation. If your result is going to be displayed in HTML, you can use the <sup></sup> tag to cause it to display in the superscript style.

As an alternative, you can have the text display as 355 cu-ft, or if you really want the superscript 3, use the unicode representation '\u00B3'. If you open your browser's web console and type '\u00B3', including the quotes, you'll get the superscript 3.

Thomas Nilsen
Thomas Nilsen
14,957 Points
System.out.println(Math.pow(3, 2)); // This is 3 to the power of 2 => 9

Sorry I mean I don't need the results. It's how to display it such as 2^ but instead of the '^' sign it it would be a number.

I'm not 100% sure how, but at least in Python you can use the double asterisk, like this: **

I tried it, it didn't work...but thanks anyways for trying to help!

No problem :P

Srry.