OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I use fixed << setprecision(2) only ONCE? Or at least restore to default behaviour?

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

Guest
This is inside of a display function. I want to print the weight using 2 decimal points. Outside of this code block, I don't want setprecision to be in effect. For example, 777.555 and 444.2222 should display correctly.

Code:
// Detect if train has cargo:
if (cargo_unit)
{
    // If cargo exists, print output:
    cout << **fixed << setprecision(2);**
    cout << "   Cargo: " << cargo_unit->getDesc() <<
    endl << "  Weight: " << cargo_unit->getWeight() << endl;
}

Problem is, once I used fixed << setprecision, I can only reset it to a number like 5 or 6 and then get this:

Code:
777.555000
444.222200
<p>This is inside of a display function. I want to print the weight using 2 decimal points. Outside of this code block, I don't want setprecision to be in effect. For example, 777.555 and 444.2222 should display correctly.</p>
<pre class="lang-cpp prettyprint-override"><code>// Detect if train has cargo:
if (cargo_unit)
{
// If cargo exists, print output:
cout << **fixed << setprecision(2);**
cout << " Cargo: " << cargo_unit->getDesc() <<
endl << " Weight: " << cargo_unit->getWeight() << endl;
}
</code></pre>
<p>Problem is, once I used fixed << setprecision, I can only reset it to a number like 5 or 6 and then get this:</p>
<pre class="lang-none prettyprint-override"><code>777.555000
444.222200
</code></pre>
Continue reading...
 

Latest posts

Online statistics

Members online
0
Guests online
2
Total visitors
2
Ads by Eonads
Top