?
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Hi,
I have a program that reads in a couple of numbers from files and then
stores them with some latex-code but I'm not satisfied with the
exponential output, as it takes up too many character spaces. Example:
4.9e+009 - takes 8 characters space (it's a double value). It should
just become 4.9e9 (takes 5 character spaces) since all my numbers are
e-positive (no such thing as e-[something]) and all my numbers are
within 0 <= x <= 9e9. I have no idea about how to do this, but can I use
ostringstream for that?
I assume I must write the exponential number to some string or stream
and then a for loop (pseudo-code):
for( i=0; i<string.length; i++)
{
if(string == 'e')
{
/* damn... What do I do? Remove 3 characters:
the "+" and 2 zeros */
}
}
outfile << exponential_number;
But there's probably a much better and efficient method.
BTW: Something went wrong, as I tried to make a minimal example for
you... I must have overseen something:
-----------
#include <iostream>
#include <iomanip> /* for std::setfill(), std::setw() */
using namespace std;
int main()
{
double d1 = 8.126e9;
setw(5);
cout << "d1 = " << setprecision(1) << ios::scientific << d1 << endl;
return 0;
}
-----------
The above program prints:
d1 = 2568e+09
???? Where did that number come from?
As said: My real program uses outfile:
outfile << " & " << setw(15) << twoDimens[j];
So once I know how how strip 3 characters from the output (after the
'e') I can implement it... Any suggestions?
Best regards
Martin Jørgensen
I have a program that reads in a couple of numbers from files and then
stores them with some latex-code but I'm not satisfied with the
exponential output, as it takes up too many character spaces. Example:
4.9e+009 - takes 8 characters space (it's a double value). It should
just become 4.9e9 (takes 5 character spaces) since all my numbers are
e-positive (no such thing as e-[something]) and all my numbers are
within 0 <= x <= 9e9. I have no idea about how to do this, but can I use
ostringstream for that?
I assume I must write the exponential number to some string or stream
and then a for loop (pseudo-code):
for( i=0; i<string.length; i++)
{
if(string == 'e')
{
/* damn... What do I do? Remove 3 characters:
the "+" and 2 zeros */
}
}
outfile << exponential_number;
But there's probably a much better and efficient method.
BTW: Something went wrong, as I tried to make a minimal example for
you... I must have overseen something:
-----------
#include <iostream>
#include <iomanip> /* for std::setfill(), std::setw() */
using namespace std;
int main()
{
double d1 = 8.126e9;
setw(5);
cout << "d1 = " << setprecision(1) << ios::scientific << d1 << endl;
return 0;
}
-----------
The above program prints:
d1 = 2568e+09
???? Where did that number come from?
As said: My real program uses outfile:
outfile << " & " << setw(15) << twoDimens[j];
So once I know how how strip 3 characters from the output (after the
'e') I can implement it... Any suggestions?
Best regards
Martin Jørgensen