how to write a function to round a float

double round(double d,unsigned pp)
{
//....
}

pp means decimal place

for example:
when pp = 0:
2.5-->3
2.3-->2

when pp=1:
3.862-->3.9
-3.869-->-3.9

Thanks!
Last edited on
Something like this should work:
1
2
double k = std::pow(10, pp);
return std::round(d * k) / k;


But note that floating point numbers can have rounding errors so the result might not be precise.
Last edited on
Really,if I write round(4.015,2),it returns 4.01,but I hope 4.02.

Then, can use bitwise operation to slove this? or any other ways to slove?

Use iomanip.h and use this:
cout<<setprecision(2)<<fixed<<showpoint<<x<<endl;
where your x is the number to round up and 2 is the number to round up to
I don't need output it. I just want to return a value to other operations.
I guess your 4.015 is actually already rounded from 4.0125. Give it a try with 4.0124 and 4.0126. One might return 4.01 and the other 4.02.
Last edited on
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

double round_number(double, int);
int pow_new(int, int number = 10);

int main ()
{
	double number;
	int places;
	
	go_back:
	
	cout << "Enter double number to round: ";
	cin >> number;
	cout << "Enter places to round: ";
	cin >> places;
	
	cout << round_number(number, places) << endl;
	
	goto go_back;
	
	return 0;
}

double round_number(double number, int places)
{
	places = pow_new(places);
	int temp_number = number * places;
	double number_2 = (double)temp_number / (double)places;
	double number_3 = (number - number_2) * places * 10.0;
	if (number_3 >= 5)
	{
		number_2 = number_2 + (1.0 / places);
	}
	return number_2;
}

int pow_new(int places, int number)
{
	if (places == 0)
	{
		return 1;
	}
	else if (places == 1)
	{
		return number;
	}
	else
	{
		return pow_new(places - 1, number * 10);
	}
}
Last edited on
Your header says something about float but none of the codes above has any. Is the problem about rounding floats or just rounding?
1
2
3
4
double round(double d, int pp) // pow() doesn't work with unsigned, so made this switch.
{ 
	return int(d * pow(10.0, pp) + .5) /  pow(10.0, pp);
}


Last edited on
Sorry for that one. :)
Thanks for help!
The code above can work well.
But some examples are not which I expect.
Such as:
number is 4.015,places are 2, I expect to get 4.02,but it is 4.01;
number is 4.2225,places are 3,I expect to get 4.223,but it is 4.222

Is this because float number can't be very precision?
Do I have any ways to slove this?
That's what your exercise is about. You cannot solve this. The reason is the binary. 1/2 + 1/4 + 1/8 + ...
Last edited on
another useful tool is fmod() from <cmath>.
http://www.cplusplus.com/reference/clibrary/cmath/fmod/
1
2
3
4
int ftoi(float f)
{
    return (int)f + ((fmod(f,1.f) < 0.5f)? 0 : 1);
}


You can play around with it for different precisions
Last edited on
solution
1
2
3
4
double round_number(double number, int places)
{
	return int(number * pow_new(places) + 0.50001) / double(pow_new(places));
}


if places are no more then 5.
Thanks!
if places are no more then 5,you advice is helpful.
But I have an another occasion:

number is 4.499999,places are 0, I expect to get 4,but it is 5
I try to subtract a very small number,but not succeed.
How should I write?
write return int(number * pow_new(places) + 0.5000001) / double(pow_new(places));
but you will get 5 if number is 4.49999999, places are 0. :D
in other words:
if you write 0.5001 number 4.4999, places 0 will be 5.
if you write 0.50001 number 4.49999, places 0 will be 5
and so on.

or
return int(number * pow_new(places) + 0.5 + 1e-16) / double(pow_new(places));
16 is your precision. On my system this is max precision.
15 places after .
Last edited on
Thanks!
but what if I convert 4.49999999 to a string to choose how to round?
You can round like that too.
I look again and found more bugs in
return int(number * pow(10.0, places) + 0.501) / double(pow(10.0, places));

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Enter double number to round: 4.3
Enter places to round: 8
4.3
Enter double number to round: 4.3
Enter places to round: 9
-2.14748
Enter double number to round: 4.3
Enter places to round: 10
-1.52297
Enter double number to round: 4.3
Enter places to round: 11
-1.76638
Enter double number to round: 4.3
Enter places to round: 12
2.95235
Enter double number to round: 4.3
Enter places to round: 13
-1.63166
Enter double number to round: 4.3
Enter places to round: 14
4.3
Enter double number to round: 4.3
Enter places to round: 15
1.40313
Last edited on
Yes, but these bugs I think is because it overflow int.
change the int to __int64 can solve.
But if places is very big, it is also unsolvable.
In fact ,I think we needn't a very big places,
for double type just have 15-16 precision .

Thanks for your help! :)
Topic archived. No new replies allowed.