I have a large number and I don't want to display it in EditText as for example 1.1E12. I want to display it in format like this: 1.1 x 10^12 (see image). Is there way to do this?
Asked
Active
Viewed 703 times
0
-
https://stackoverflow.com/questions/32722349/java-output-double-numbers-in-exponential-format – Software Engineer May 08 '20 at 13:37
-
@Software Engineer It does not answer my question because I don't want to display it using E. I want to have nice output like in math books when kids use exponents. As I've shown in image. – Antonio May 08 '20 at 13:52
-
1I think it does -- it says that there isn't any way to achieve this using a built-in function. You have to write code. And if you want help with that here then you have to try it yourself first and ask us for help when you're stuck with some of it by posting a minimal example. – Software Engineer May 08 '20 at 13:54
-
@SoftwareEngineer I know that I should provide some code, but I have no idea even how to begin with that so if you have any ideas I would be thankful for any help – Antonio May 08 '20 at 13:57
-
1That's not really what StackOverflow is for. We help people with problems they're having with their code, but we don't often write code for them. This question is likely to be closed in its current state. This is because if you don't know where to start then we'd have to teach you the fundamentals of programming first, which is too big an ask for a forum like this. – Software Engineer May 08 '20 at 13:59
1 Answers
2
I think you are asking how to generate a string that represents a number in “math textbook” scientific notation, like 6.02214076×10²³.
You can split the number into its base and exponent using Math.log10, then convert the exponent’s digits to Unicode superscript characters:
public static String formatInScientificNotation(double value) {
NumberFormat baseFormat = NumberFormat.getInstance(Locale.ENGLISH);
baseFormat.setMinimumFractionDigits(1);
if (Double.isInfinite(value) || Double.isNaN(value)) {
return baseFormat.format(value);
}
double exp = Math.log10(Math.abs(value));
exp = Math.floor(exp);
double base = value / Math.pow(10, exp);
String power = String.valueOf((long) exp);
StringBuilder s = new StringBuilder();
s.append(baseFormat.format(base));
s.append("\u00d710");
int len = power.length();
for (int i = 0; i < len; i++) {
char c = power.charAt(i);
switch (c) {
case '-':
s.append('\u207b');
break;
case '1':
s.append('\u00b9');
break;
case '2':
s.append('\u00b2');
break;
case '3':
s.append('\u00b3');
break;
default:
s.append((char) (0x2070 + (c - '0')));
break;
}
}
return s.toString();
}

VGR
- 40,506
- 4
- 48
- 63