/* ************************************************************************** * Program name : 031_Rounding (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/12 - first version * ************************************************************************** * Description : (a) Input a real number * * (b) Output the number in different format * * (c) Try again ? * ************************************************************************** */ #include #include #include #include int main() { // part 1 : declaration double Number, Div, Temp; int Index; char Again; do { // part 2 : Repeat asking a positive real number do { cout << "\nPlease enter a positive real number : "; cin >> Number; } while (Number<=0); // part 3 : Calculation and output results cout << "\nThe inputted number can be round up to : "; for (Index=4 ; Index >= 1; Index--) // Note (1) { if (floor(Number / pow(10,Index-1)) != 0) cout << setprecision(0) << resetiosflags(ios::showpoint) << "\n" << floor((Number+0.5*pow(10,Index-1)) / pow(10,Index-1)) *pow(10,Index-1); } for (Index=1 ; Index <= 10; Index++) // Note (2) cout << setprecision(Index) << setiosflags(ios::fixed |ios::showpoint) << "\n" << floor(floor(Number*pow(10,Index+1)+5)/10)/pow(10,Index); // part 4 : try another number ? cout << "\n\n\aTry another number (Y/N) : "; cin >> Again; cout << "\n"; } while (Again=='Y' || Again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) Round up to interger of Thousand, Hundred and Ten etc. (a) The statement "setprecision(0)" set the ouput without decimal digit. (b) The statement "resetiosflags(ios::showpoint)" set the ouput without decimal point. (c) Example : Let us say the inputted number is 456.5639 ---------------------------------------------------------------------------- Index | 4 | 3 | 2 | 1 ---------------------------------------------------------------------------- pow(10,Index-1) | 1000 | 100 | 10 | 1 ---------------------------------------------------------------------------- Number+0.5*pow(10,Index-1) | 956.XX | 506.XX | 461.XX | 457.XX ---------------------------------------------------------------------------- floor((Number+0.5*pow(10,Index-1)) | | | | / pow(10,Index-1)) | 0 | 5 | 46 | 457 ---------------------------------------------------------------------------- floor((Number+0.5*pow(10,Index-1)) | 0*1000 | 5*100 | 46*10 | 457*1 / pow(10,Index-1)) *pow(10,Index-1); | = 0 | = 500 | = 460 | = 457 ---------------------------------------------------------------------------- The output is : --------------------------- 500 460 457 (2) Round up to nearest Ten, Hundred and Thousand etc of deciaml (a) The statement "setprecision(Index)" set the no. of decimal digits. (b) The statement "setiosflags(ios::fixed |ios::showpoint)" set the output format of float with decimal point and fixed number of decimal digits. (c) Example : Let us say the inputted number is 456.5639 ---------------------- Index | Output ---------------------- 1 | 456.6 2 | 456.56 3 | 456.564 4 | 456.5639 5 | 456.56390 ---------------------- */