E
eriwik
I'm trying to write floats to a file following a specific format (8
characters/record, and some other rules) and seem to have solved all
but one problem. There is no way of telling the maximum characters to
output. For positive numbers this is no problem since I can specify the
precision (number of digits not counting the decimal-point), however
when the number is negative the minus-sign does not count towars the
precision (which it shouldn't really), which has the effect that a
value that is written as 8 characters when positive is 9 when negative.
The following illustrates the problem:
#include <iostream>
#include <ios>
int main()
{
std::cout.precision(7);
std::cout.setf(std::ios_base::showpoint);
std::cout << 54.325617 << " ";
std::cout << -54.325617 << " ";
std::cout << 45.545 << " ";
float f = 3434;
std::cout << f << " ";
return 0;
}
This outputs "54.32562 -54.32562 45.54500 3434.000".
All but the second number is 8 characters long, does anyone have a
solution to this or will I have to make a test before writing each
number and see if it's negative, and then reduce precision to 6?
characters/record, and some other rules) and seem to have solved all
but one problem. There is no way of telling the maximum characters to
output. For positive numbers this is no problem since I can specify the
precision (number of digits not counting the decimal-point), however
when the number is negative the minus-sign does not count towars the
precision (which it shouldn't really), which has the effect that a
value that is written as 8 characters when positive is 9 when negative.
The following illustrates the problem:
#include <iostream>
#include <ios>
int main()
{
std::cout.precision(7);
std::cout.setf(std::ios_base::showpoint);
std::cout << 54.325617 << " ";
std::cout << -54.325617 << " ";
std::cout << 45.545 << " ";
float f = 3434;
std::cout << f << " ";
return 0;
}
This outputs "54.32562 -54.32562 45.54500 3434.000".
All but the second number is 8 characters long, does anyone have a
solution to this or will I have to make a test before writing each
number and see if it's negative, and then reduce precision to 6?