Sora - 3 months ago 19
Vb.net Question

# Math.Round in vb.net not working as expected

I have this number :

`666872700`
i need to round it to be :
`666900000`

I used :
`Math.Round(666872700,4)`
but this is not working
Is there any easy way in vb.net I can use other than dividing by
`100000`
then rounding then multiplying by
`100000`
?

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;
}
}
``````
Source (Stackoverflow)