# [solved] Roundup value to nearest 200

• Hi,
I need to roundup a value which is always a positive integer. I have to round up it to the nearest 200.
Rules are;

1. Value should be rounded-up to nearest 200. (if 350 I need 400 and if 250 I need 200)
2. Minimum value should be 200. (even 1 should give me 200)

Can anybody help me?

• use the modulo operator "%".
if the value returned by modulo is smaller than 100 (half of 200) subtract the mod-value from the current value, otherwise add (200-moduloValue) to your current value.

You just need a special case handling for 2)

• raven-worx,

I created below method. Can you check is it the best way for do that?

@void setLength(int l)
{
int minimum_length = 200;

``````if(l<minimum_length)
{
l=minimum_length;
}
else
{
int mod_200 =l % 200 ;

if(mod_200!=0)
{
l = (l/200 + qRound(mod_200/100))*200;
}
}

d->length = l;
``````

}@

Thanks again.

• have you tested your method?!

Your method doesn't behave like you described it should be.
Neither did you implement what i suggested.

Here is the working method:
@
int roundValue(int l)
{
const int mod = 200;

``````if(l < mod)
{
l = mod;
}
else
{
int modVal = l % mod;

if( modVal < (mod/2.0) )
l -= modVal;
else
l += (mod-modVal);
}

return l;
``````

}
@

• yes I tested it and it is working for me. :)

give minimum 200 and round value to nearest 200.