Here is my code:
double round( char* strNumber, int decPlace);
int main()
{
int decimal;
char initialNumber[256];
cout << "Enter decimal and number " << endl;
cin >> decimal;
cin >> initialNumber;
cout << setprecision (15) << round ( initialNumber,decimal ) << endl;
return 0;
}
double round( char* strNumber, int decPlace)//
{
double number = atof(strNumber);
int temp = ( int ) ( pow(10.0,decPlace) * number + 0.5 );
double result = ( double ) temp / pow(10.0,decPlace);
return result;
}
It works up to 6 decimal places. Otherwise it gives some strange result. Below are numbers that I used for testing and the output:
Test 1-round to 7 decimal places
105.265
52.5689745694
25.6835
452.689785
12.456789877458
Output
105.265
52.5689746
25.6835
-214.7483648
12.4567899
Test 1-round to 8 decimal places
The same numbers as previously
Output
-21.47483648
-21.47483648
-21.47483648
-21.47483648
12.45678988