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, asf = 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.
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
Why not use string parsing? scan the string and replace e with 10^.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With