"Niranjan Kulkarni" <guruof...@...> wrote:
>
> Hi Everyone,
>
> I am witting a function in C++ for any given number to round
> upto the iRoundTo.
> Here is the function I have written:
>
> void RoundUpTo(float& Num, int iRoundUpTo)
> {
> int iRem = 0;
> int iNum = ceil(Num);
This defeats the purpose of using a float.
> //Taking another variable since Num
> float variable errors out in mod operation below.
Use fmod.
> if(iNum == 0)
> return;
>
> iRem = iNum % iRoundUpTo;
> int iFactor = iRoundUpTo - iRem;
> iNum += iFactor;
> Num = (float)iNum;
The cast is redundant.
> }
>
> The expected results is something as follows:
> 121.23, 10 ==> 130.00
> 121.23, 100 ==> 200.00
> 121.23, 1000 ==> 1000.00
> 121.23, 50 ==> 150.00
> 3, 10 ==> 10.00
> 3, 100 ==> 100.00
> 0.00, 10 ==> 0.00
> 0.00, 100 ==> 0.00
#include <iostream>
#include <cmath>
using namespace std;
double f(double a, double b)
{
double r = fmod(a, b);
double c = r ? ceil(a/b) * b : a;
cout << a << ", " << b << " ==> " << c << endl;
return c;
}
int main()
{
f(121.23, 10);
f(121.23, 100);
f(121.23, 1000);
f(121.23, 50);
f(3, 10);
f(3, 100);
f(0.00, 10);
f(0.00, 100);
f(3.141592, 2.2);
}
--
Peter