:?: how do I get the high-lighted lines below to round up to just two (2) decimal places using the setprecision

// Program description: Calculate the price, including taxes, of a water bill
// given the amount of water in gallons
// Author: Eric Martin
// Date: 10.21.2004

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

int main (){
    // Declare identifiers

    float Water;
    float Srate;
    float Wrate;
    float Gallons;
    float Taxes;
    float Total1;
    float Total2;

    // Initiailize identifiers

    cout << " How much water was used in one month?: ";
    cin >> Water;

    // Formulate
    Gallons = Water / 100;
    Wrate = Gallons * .021;
    Srate = Gallons * .001;
    Total1 = Wrate + Srate;
    Taxes = Total1 * .02;
    Total2 = Total1 + Taxes;

    std::cout << " The amount of water used this month----------" << Gallons << endl;

    [COLOR=Red]std::cout << " Amount due for use of water----------  " << setprecision(0) << Wrate << endl;

    std::cout << " Amount due for sewage----------  "<< setprecision(0) <<  Srate << endl;

    std::cout << " Total water and sewage charge---------- " << setprecision(0) << Total1 << endl;

    std::cout << " Total amount of taxes----------  " << setprecision(0) << Taxes << endl;

    cout << " TOTAL AMOUNT DUE----------  " << setprecision(0) << Total2 << endl;[/COLOR]   getch();
    return 0;

Dani AI

Generated

Brief note for : correctly identified that the visible problem is output formatting. Below are practical, production-minded suggestions that avoid floating‑point surprises when dealing with money and ensure a stable two‑decimal display.

Do calculations in cents (integers) and round once when converting from a floating result to cents. That removes binary rounding noise and simplifies printing two decimals reliably. Example approach (read input, compute dollar amounts as doubles, convert + round to integer cents, then print dollars and two-digit cents):

#include <iostream>
#include <cmath>
#include <iomanip>

double readUsage(); // your input logic

// ... compute water_dollars and sewage_dollars as doubles ...
long water_cents  = static_cast<long>(std::round(water_dollars  * 100.0));
long sewage_cents = static_cast<long>(std::round(sewage_dollars * 100.0));

long subtotal_cents = water_cents + sewage_cents;
long tax_cents      = static_cast<long>(std::round(subtotal_cents * 0.02)); // 2% tax
long total_cents    = subtotal_cents + tax_cents;

// print without changing global numeric flags
std::cout << "Water: " << (water_cents/100) << '.'
          << std::setw(2) << std::setfill('0') << (labs(water_cents)%100) << '\n';

A few extra tips:

  • Prefer integer cents (int64_t) for sums and ledger storage; use doubles only for rate math and conversions.
  • Decide whether to round each line item to cents before tax or round the tax itself — rules/requirements vary by jurisdiction.
  • Replace nonportable headers like conio.h/getch() and validate input.
  • If you only need to format output once, composing the formatted string in an ostringstream avoids changing ostream state globally.

These practices give correct two‑decimal output while avoiding hidden rounding errors that float displays can mask.

Greetings dcving,

Fixing this issue is quite simple. Let me expound.

By using setprecision, you asked for two places, and it printed two places. If instead you want two decimal places (positions to the right of the decimal point), you must first choose the fixed-point format. For example:

cout << setiosflags(ios::fixed) << setprecision(2) << x;

The default C++ stream behavior for setprecision(n) is to apply the specification to the entire number, not the fractional part. If this is not what you want, set the fixed-point format first. Furthermore, setiosflags sets the format flags specified by parameter mask. Here is a list of the flags and their description:

[b][u]flag[/u][/b]			[b][u]effect if set[/u][/b]
ios_base::boolalpha	input/output bool objects as alphabetic names (true, false).
ios_base::dec		input/output integer in decimal base format.
ios_base::fixed		output floating point values in fixed-point notation.
ios_base::hex		input/output integer in hexadecimal base format.
ios_base::internal	the output is filled at an internal point enlarging the output up to the field width.
ios_base::left		the output is filled at the end enlarging the output up to the field width.
ios_base::oct		input/output integer in octal base format.
ios_base::right		the output is filled at the beginning enlarging the output up to the field width.
ios_base::scientific	output floating-point values in scientific notation.
ios_base::showbase	output integer values preceded by the numeric base.
ios_base::showpoint	output floating-point values including always the decimal point.
ios_base::showpos	output non-negative numeric preceded by a plus sign (+).
ios_base::skipws	skip leading whitespaces on certain input operations.
ios_base::unitbuf	flush output after each inserting operation.
ios_base::uppercase	output uppercase letters replacing certain lowercase letters.

Hope this helps,
- Stack Overflow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.