tags:

views:

109

answers:

1

hi, how i can convert decimal value to exponential value

A: 
float x = 12313.234;
boolean neg = x < 0.0;
x *= neg ? -1 : 1;
int c = 0;
String exp;
if(x >= 1.0)
{
  while(x > 10.0)
  {
    x /= 10.0;
    c++;
  }
  exp = String.valueOf(x) + "e+" + String.valueOf(c);
}
else
{
  while(x < 1.0)
  {
    x *= 10.0;
    c++;
  }
  exp = String.valueOf(x) + "e-" + String.valueOf(c);
}
exp = neg ? ("-" + exp) : exp;
System.out.println(exp);
Amarghosh
You sir, are a maniac.
MattC
Yeah, now that I know that it can be done with the one liner `System.out.printf("%e\n", x);`, I can't agree more with you: I am a maniac.
Amarghosh