Rounding off Decimal

Posted by Raj.Trivedi under C# on 11/17/2014 | Points: 10 | Views : 327 | Status : [Member] [MVP] | Replies : 1
Hello friends

I am performing calculations

and i get results as

1230.4896

or

7896.5236

What i want is if the value after decimal is less than .50XX then it should be converted to the lesser whole number as in above case it should get value as 1230 and if the value after is greater than or equal to .50XX then it should get converted to 7897

Any reference will be appreciated

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved



Responses

Posted by: Bandi on: 11/17/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Basically you give the Math.Round method three parameters.

The value you want to round.
The number of decimals you want to keep after the value.
An optional parameter you can invoke to use AwayFromZero rounding. Without it, '1.5' rounds to '1' instead of '2'.
Sample code:

var roundedA = Math.Round(1.1, 0); // Output: 1
var roundedB = Math.Round(1.5, 0, MidpointRounding.AwayFromZero); // Output: 2
var roundedC = Math.Round(1.9, 0); // Output: 2

You need MidpointRounding.AwayFromZero is you want a .5 value to be rounded up. Unfortunately this isn't the default behavior for Math.Round().

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Raj.Trivedi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response