Sora Sora - 1 year ago 179 Question

Math.Round in not working as expected

I have this number :

i need to round it to be :

I used :
but this is not working
Is there any easy way in I can use other than dividing by
then rounding then multiplying by

Answer Source

The documentation for Math.Round clearly states:

Rounds a double-precision floating-point value to a specified number of fractional digits.

So it rounds stuff behind the decimal separator, but not integral part. I know of no other way than dividing, rounding and then multiplying again.

If you know a little C#, you can use the following extension method Jason Larke wrote in his answer to this question. I don't know whether it works, but you should be able to translate it to VB.NET and try it:

public static class MathExtensions
    public static int Round(this int i, int nearest)
        if (nearest <= 0 || nearest % 10 != 0)
            throw new ArgumentOutOfRangeException("nearest", "Must round to a positive multiple of 10");

        return (i + 5 * nearest / 10) / nearest * nearest;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download