Answered by:
How to round up figures.
Question

User2097295820 posted
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.
Tuesday, May 3, 2016 12:46 PM
Answers

User753101303 posted
Hi,
Have you tried Math.Round(value,2,MidpointRounding.AwayFromZero) ? Try https://msdn.microsoft.com/enus/library/system.midpointrounding(v=vs.110).aspx for details.
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 3, 2016 12:59 PM
All replies

User753101303 posted
Hi,
Have you tried Math.Round(value,2,MidpointRounding.AwayFromZero) ? Try https://msdn.microsoft.com/enus/library/system.midpointrounding(v=vs.110).aspx for details.
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 3, 2016 12:59 PM 
User2097295820 posted
Thanks alot bro. I have researched on this but did not get it.
Tuesday, May 3, 2016 2:31 PM 
User1940134294 posted
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.14double 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.Friday, May 6, 2016 5:49 AM