Link to home
Start Free TrialLog in
Avatar of Anjala Baby
Anjala BabyFlag for India

asked on

Rounding off Issue using Math.Round()

Hi Experts,

I have tried to round off a value x = 455.77 using math .round() function but not rounded the value 466.

Code I have given to round off:

var rounded = Math.Round(x, 2, MidpointRounding.AwayFromZero);

Open in new window


Could you guys please explain whats wrong here?

Thanks,
Anjala Baby
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

The '2' specifies you want the result to 2 decimal places.  If you want it rounded to give 466 then you must use
            var rounded = Math.Round(x, 0, MidpointRounding.AwayFromZero);
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Anjala Baby

ASKER

So, How the value 455.50 will be rounded if we use '0' in round function?
Do you want 456.00 for this?
No, I just want to know how  the result will be when we give value as 455.5. Will it be 455 or 456?
It will give you 456.0
You could easily try that yourself but see this:-
User generated image
If you use this ROUND(455.45,0) then you will get 455.00
Thank you guys for the answers.