java 输出一个数字的幂作为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12208275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:02:43  来源:igfitidea点击:

Output a number raised to a power as a string

javaandroidnumbers

提问by user1246462

I am new to Java and I'm not quite sure how to output an integer raised to a power as a string output. I know that

我是 Java 新手,我不太确定如何输出一个整数作为字符串输出。我知道

Math.pow(double, double)

will actually compute the value of raising a double to a power. But if I wanted to output "2^6" as an output (except with 6 as a superscript and not with the carat), how do I do that?

将实际计算将 double 提高到幂的值。但是如果我想输出“2^6”作为输出(除了 6 作为上标而不是克拉),我该怎么做?

EDIT: This is for an Android app. I'm passing in the integer raised to the power as a string and I would like to know how to convert this to superscript in the UI for the phone.

编辑:这是一个 Android 应用程序。我将整数作为字符串传递给幂,我想知道如何将其转换为手机用户界面中的上标。

回答by Thilo

Unicode does have superscript versions of the digits 0 to 9: http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts

Unicode 确实有数字 0 到 9 的上标版本:http: //en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts

This should print 2?:

这应该打印 2?:

 System.out.println("2?");

 System.out.println("2\u2076");

回答by DaoWen

If you're outputting the text to the GUI then you can use HTML formattingand the <sup>tagto get a superscript. Otherwise, you'll have to use Unicode characters to get the other superscripts. Wikipedia has a nice article on superscripts and subscripts in Unicode:

如果您将文本输出到 GUI,那么您可以使用HTML 格式<sup>标签来获取上标。否则,您将不得不使用 Unicode 字符来获取其他上标。维基百科有一篇关于 Unicode 上标和下标的好文章:

http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts

http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts

回答by TheNewGuy

This answer should only be used if using Eclipse (a java editor) what eclipse does is it only supports certain Unicode symbols. 1 2 and 3 can all be done and are supported by eclipse, anything else you have to play with the settings of eclipse which isnt too hard. There's this thing called Windows-1252 which is the default for a lot of stuff it seems, including being the default encoding for files in Eclipse. So, whenever you're printing to the console, that's what it's trying to interpret it as, and it doesn't support the full unicode charset since it's a 1 byte encoding. This isn't actually a problem with the Java, then, it's a problem with Eclipse, which means you need to edit your Eclipse to use UTF-8 instead. You can do this in multiple places; if you just want the unicode characters to be displayed when you're running this one file, you can right click the file, properties -> resource -> text file encoding and change from default to other: utf-8. If you want to do this to your whole workspace, you can go to Window -> Preferences -> General -> Workspace -> Text file encoding. You could also do this project-wide or even just package-wide following pretty similar steps depending what you're going for.

仅当使用 Eclipse(java 编辑器)时才应使用此答案,eclipse 的作用是仅支持某些 Unicode 符号。1 2 和 3 都可以完成并得到 eclipse 的支持,其他任何你必须使用 eclipse 设置的东西都不太难。有一个叫做 Windows-1252 的东西,它似乎是很多东西的默认设置,包括作为 Eclipse 中文件的默认编码。因此,每当您打印到控制台时,这就是它试图将其解释为的内容,并且它不支持完整的 unicode 字符集,因为它是 1 字节编码。这实际上不是 Java 的问题,那么,这是 Eclipse 的问题,这意味着您需要编辑 Eclipse 以改用 UTF-8。您可以在多个地方执行此操作;如果您只想在运行这个文件时显示 unicode 字符,您可以右键单击该文件,属性 -> 资源 -> 文本文件编码并从默认更改为其他:utf-8。如果要对整个工作区执行此操作,可以转到窗口 -> 首选项 -> 常规 -> 工作区 -> 文本文件编码。您也可以按照非常相似的步骤在整个项目范围内执行此操作,甚至只是在包范围内执行此操作,具体取决于您的目的。