Rouding Function

I have tried a c++ rounding function and could only round a number to the nearest integer.How to round a long double to the number of decimals specified by the user?NB:Do not use build in functions.
See this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <conio.h>

using namespace std ;

int main()
{
	long double num,decimal,temp;
	int rd,i;

	cout << "Enter the no. and rounding digit :";
	cin>>num>>rd;
	i=rd;

	temp=(int)num;
	decimal=num-temp;

	while(i)
	{
		decimal*=10;
		i--;
	}
	decimal=(int)decimal;
	i=rd;
	while(i)
	{
		decimal*=0.1;
		i--;
	}

	num=(int)num+decimal;
	cout.setf(ios::fixed,ios::floatfield);  
	cout<<"The rounded number is :"<<num;
	_getch();
	return 0;

}
If you will have any problem with

cout.setf(ios::fixed,ios::floatfield);

then read this :

http://www.cplusplus.com/reference/iostream/ios_base/precision/
Topic archived. No new replies allowed.