//This program rounds a number to the nearest hundreths //File name Round.cpp #include using namespace std; int main() { //first code section does conversion line by line and outputs to show what is happening float myBill; int myBillasInt; myBill = 45.76891; //test value with too many decimal places cout << "Rounding an amount to nearest dollars and cents in multiple steps\n"; cout << myBill << endl; myBill = myBill + 0.005; //cause rounding to cents cout << myBill << endl; myBill = 100*myBill; //this statement keeps only the dollars and cents part of the number cout << myBill << endl; myBillasInt = myBill; //type cast to int, losing the unwanted decimal places cout << myBillasInt << endl; myBill = (float)myBillasInt/100.0; //restore bill to its normal value cout << myBill << endl; //second code section does conversion in one statement using type casting and coercion cout << "\nRounding an amount to the nearest dollars and cents in one statement\n"; myBill = 45.76891; cout << "The original computed bill is " << myBill << endl; myBill = int((myBill + 0.005)*100)/100.0; //this statement does everything! cout << "My rounded bill is " << myBill << endl; return 0; }