473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Formating of floats using iostream

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?

--
Erik Wikström

Nov 17 '06 #1
4 3391
er****@student.chalmers.se wrote:
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?
Use std::setw in <iomanip>.

Cheers! --M

Nov 17 '06 #2
On 17 Nov, 14:01, "mlimber" <mlim...@gmail.comwrote:
eri...@student.chalmers.se wrote:
[snip]
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?

Use std::setw in <iomanip>.
Yeah, so I thought at first, but according to the standard that's the
same as doing std::cout.width(8), which in turn sets the _minimul_
field width, not max. So it basically just tells the stream how many
fill-characters to put in if the field is too small :-(

--
Erik Wikström

Nov 17 '06 #3
er****@student.chalmers.se wrote:
On 17 Nov, 14:01, "mlimber" <mlim...@gmail.comwrote:
eri...@student.chalmers.se wrote:
[snip]
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?
Use std::setw in <iomanip>.

Yeah, so I thought at first, but according to the standard that's the
same as doing std::cout.width(8), which in turn sets the _minimul_
field width, not max. So it basically just tells the stream how many
fill-characters to put in if the field is too small :-(
If you must maintain the 8 character width, drop the precision by 1,
either universally or only on negative numbers:

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

class HardWidth
{
public:
HardWidth( const double f, const unsigned width=8 )
{
const unsigned prec = ( f < 0 ? width - 2 : width - 1 );
m_oss << showpoint << setw( width-1 ) << setprecision( prec ) << f;
}

friend inline std::ostream& operator<<( std::ostream &os, const
HardWidth &h )
{
return os << h.m_oss.str();
}

private:
ostringstream m_oss;
};

int main()
{
cout << HardWidth( 12.3456789 ) << " ";
cout << HardWidth( -12.3456789 ) << " ";
return 0;
}

// Output: 12.34568 -12.3457

You could also turn on the plus sign by using std::showpos.

Cheers! --M

Nov 17 '06 #4
mlimber wrote:
er****@student.chalmers.se wrote:
>On 17 Nov, 14:01, "mlimber" <mlim...@gmail.comwrote:
>>eri...@student.chalmers.se wrote:
[snip]
>>>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?
Use std::setw in <iomanip>.
Yeah, so I thought at first, but according to the standard that's the
same as doing std::cout.width(8), which in turn sets the _minimul_
field width, not max. So it basically just tells the stream how many
fill-characters to put in if the field is too small :-(

If you must maintain the 8 character width, drop the precision by 1,
either universally or only on negative numbers:
Then I'd argue that's a flaw in iostream formatting. In printf (yes, I
know it's not typesafe), you can specify %X.Ylf format. The amount of
hoop-jumping needed for setting widths and formatting in iostreams is
excessive.

Mind you, I *LIKE* iostreams. They're very useful, and much cleaner
than printf .... EXCEPT for field formatting on primitive types.
Nov 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: J.K. Becker | last post by:
Hi there, I am trying to multiply doubles with floats (actually I tried every possible combination by now) and it never works (well, it does something but it is always wrong). I have no idea...
12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
6
by: sbalko | last post by:
Hi, I am trying to read Java-floats (IEEE 754 encoding) stored in a binary file from C (gcc on linux/i386, more specifically). Unfortunately, C seems to expect floats to be stored somewhat...
2
by: Matt McGonigle | last post by:
Hi all, Please help me out with this. Perhaps it is a dumb question, but I can't seem to make it work. I am doing a file conversion using an unformatted binary file for input and outputting to...
1
by: Theadmin77 | last post by:
I have to validate s floats thru a function , but the validation have to be don with a string of 20 characters .... Any idea how to do thatvalidation floats? Any help will be very appreciated...
16
by: luca bertini | last post by:
Hi, i have strings which look like money values (ie 34.45) is there a way to convert them into float variables? everytime i try I get this error: "numb = float(my_line) ValueError: empty string...
2
by: sitko | last post by:
Hi, I'm in the process of converting a VB.net program into a C program so it can run on a unix like machine. I've been moving along at a nice pace, but this conversion has stumped me. I need...
1
by: donpro | last post by:
https://testbed.odysseyshipping.com/index.php This is driving me nuts. I've spent much time trying to style this page footer but because I cannot set widths using "display: inline". I've tried...
1
by: efittery | last post by:
I need to modify the following code to handle a vector of pairs of floats. I was just thinking of casting my vector of floats into being a vector of pairs of floats. Alternately, I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.