Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing float such that exponent is marked with "*10^" instead of "e"

I am looking for a possibility within C/C++ to print a float (or double) f, say f = 1.234e-15, such that it is printed as

  • f = 1.234*10^-15, or, even better, as
  • f = 1.234*10^{-15}

Can anyone help me? Maybe there is a way to get the exponent "-15" and mantissa "1.234" in base 10. I found the question how can I extract the mantissa of a double, but unfortunately that did not really help, since it only gets the mantissa in base 2.

like image 937
alex Avatar asked Dec 16 '22 22:12

alex


2 Answers

You can print to a string using the output string stream, and then replace "e" with "*10^".

ostringstream ss;
ss << scientific << 123456789.87654321;
string s = ss.str();
s.replace(s.find("e"), 1, "*10^");
cout << s << endl;

This snippet produces

1.234568*10^+08
like image 149
Sergey Kalinichenko Avatar answered Mar 29 '23 23:03

Sergey Kalinichenko


Why not use string parsing? scan the string and replace e with 10^.

like image 29
edison Avatar answered Mar 30 '23 00:03

edison