Is it possible in Java, like in Word, to write a number to a power in a variable of type String . With this count it? I know that it is possible to write the power of a number as:

1.0E20

If there are no standard features, is it possible to write your own parser?

UPDATE

There are some long numbers that I want to write in the format 1.11 * 10 ^ 12 . Is it possible to record the degree as in the Word at the top. Display in EditText .

  • More specifically, what do you want to do? What should be the input and what is the output (with examples)? - Nofate
  • @Nofate so clear? - RodGers
  • Write down where? When outputting to the console? In the swing application? - Nofate
  • @Nofate in Android EditText - RodGers

1 answer 1

As an option - you can use the Html class to format the text:

 private static CharSequence formatPower(String source) { int powerPosition = source.indexOf("E"); if (powerPosition == -1) { return source; } String numberValue = source.substring(0, powerPosition); String powerValue = source.substring(powerPosition + 1); return fromHtml(numberValue + "<sup>" + powerValue + "</sup>"); } private static Spanned fromHtml(String html) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY); } else { return Html.fromHtml(html); } } 

And further:

 mEditText.setText(formatPower("1.0E20")); 
  • Thank you so much! Everything is working. - RodGers