Keselme
Keselme

Reputation: 4249

Show a number raised to the power of another number - superscript

In my application I want to show a textview of a number raised to the power of another number. For example 2^3 but with out the caret. My actual number is in scientific form - 3.488807993e-5 and I want to show it as 3.4^e-5. This is what I tried

@Override
    public String getFormattedValue(float value, AxisBase axis) {
        String temp = String.format("%.2e", value);
        String base = temp.substring(0, temp.indexOf('e'));
        String power = temp.substring(temp.indexOf('e'));
        return base + "<sup>" + power + "</sup>";
    }

However, it didn't work.

Upvotes: 0

Views: 296

Answers (1)

diginoise
diginoise

Reputation: 7620

Firstly please keep in mind that 3.4e-5 means 3.4 * 10-5 so in your formatting it would be 3.4*10^-5

Then your code could looks something like this (HALF_UP rounding and default locale):

String temp = "3.45678e-5";
double base = Double.parseDouble(temp.substring(0, temp.indexOf('e')));  //need double
String power = temp.substring(temp.indexOf('e') + 1);
System.out.printf("%.2f*10^%s",base, power);

which prints 3.46*10^-5.

For Android formatting you could use (with variables slotted in):

((TextView)findViewById(R.id.text)).setText(
    Html.fromHtml("3.46*10<sup><small>-5</small></sup>")
); 

Upvotes: 1

Related Questions