Formatting output .9 instead of 0.9?

I am using borland C++ 4.52. I need to format some output that will show a

double to 3 digit percision

and truncate the leading zero when there.

I keep getting 0.999 instead of .999 . any suggestions?


price purchase and traveled are doubles pulled in from a file where they are

space deliniated

i.e.

.999 13.5 285.6

1.019 14 275

1.159 16.75 299.4

the following is the code that will handle the printout that I am having

trouble with.




cout.setf(ios::fixed,ios::fixed); //setting the fixed float fields

cout.setf(ios::left,ios::adjustfield); //setting the fixed float fields

do

{

if (fin>>price)

{

fin>>purchase>>traveled;

cout<<"
"<<setw(22)<<"Gas Price Per Gallon"<<"=<br>
$"<<setprecision(3)<<price<<endl; //THIS IS THE LINE I NEED HELP WITH<br>
cout<<setw(22)<<"Total Purchase Amount"<<"=<br>
$"<<setprecision(2)<<purchase<<endl;<br>
cout<<setw(22)<<"Total Miles Travelled"<<"=<br>
"<<setprecision(1)<<traveled<<endl;<br>
cout<<setw(22)<<"Miles Per Gallon"<<"=<br>
"<<setprecision(0)<<mpg(traveled,price,purchase)<<endl;<br>
}

while( );




Comments

  • : I am using borland C++ 4.52. I need to format some output that will show a

    : double to 3 digit percision

    : and truncate the leading zero when there.

    : I keep getting 0.999 instead of .999 . any suggestions?


    There is no adequate solution that I can see by setting a flag or using a manipulator. A floating point output begins with a 0. if it is between -1 and 1.


    If you really need to do this, though, there is hope. Output the number to a string instead (use an ostrstream object instead of cout, making all your precision and width modifications to it instead... don't forget to stream in an ends (end of string)!). Then, get rid of the 0 yourself if it exists, in the string. Then, output the string.


    Best solution I can come up with on short notice.




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories