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
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?
write returnint(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 returnint(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 .
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
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 .