 ## 3 replies

Last post May 06, 2016 05:49 AM by bnarayan

• ### How to round up figures.

I want to round up in .net and it just does not seem to work.

Let say 3.145.  I want to round this 3.15.

I use the the Math.round() and I get    3.14

Is there any algorithm that can help me round up accurately in .net.

3.145   to  3.15

I need this because I am sending values to payment gateway. There as to be accuracy.

• ### Re: How to round up figures.

Hi,

Have you tried Math.Round(value,2,MidpointRounding.AwayFromZero) ? Try  https://msdn.microsoft.com/en-us/library/system.midpointrounding(v=vs.110).aspx for details.

• ### Re: How to round up figures.

Thanks alot bro. I have researched on this but did not get it.

• ### Re: How to round up figures.

It seems that you are using the float data type that’s why you are not getting the desired result.

If you use double data type it will provide you the required result.

```float a = 3.145f;
Math.Round(a, 2, MidpointRounding.AwayFromZero);```

Output
3.14

```double a = 3.145;
Math.Round(a, 2, MidpointRounding.AwayFromZero);
```

Output

3.15

It happens because when you convert from float to double you will not get the exact data.

e.g.

```float a = 3.145f;
double b = Convert.ToDouble(a); Write(b); ```

value of b: 3.14499998092651

you can see that the value is not exactly same when converting from float to double. that's why you may get undesired result.

Best Regards,
Banketeshvar Narayan