Decimal round

  • Thread starter Thread starter Luigi Z
  • Start date Start date
L

Luigi Z

Hi all,
how can I write a method that returns a rounded decimal like these rules?

0.49 -> 0
0.50 -> 0
0.51 -> 1
and so on for positive values
-0.49 -> 0
-0.50 -> 0
-0.51 -> -1
and so on for negative values.

Thanks in advance.

Luigi
 
Hi all,
how can I write a method that returns a rounded decimal like these rules?

0.49 -> 0
0.50 -> 0
0.51 -> 1
and so on for positive values
-0.49 -> 0
-0.50 -> 0
-0.51 -> -1
and so on for negative values.

Thanks in advance.

Luigi

Any particular aversion to using Math.Round()?
You can even use MidpointRounding.ToEven or
MidpointRounding.AwayFromZero to push the rounding in the direction
you need.

You might want to try searching MSDN.Microsoft.com first.

Tom P.
 
Tom P. said:
Any particular aversion to using Math.Round()?
You can even use MidpointRounding.ToEven or
MidpointRounding.AwayFromZero to push the rounding in the direction
you need.

You might want to try searching MSDN.Microsoft.com first.

Thank you for response Tom, but I've not found a solution yet.

Luigi
 
Luigi said:
Hi all,
how can I write a method that returns a rounded decimal like these rules?

0.49 -> 0
0.50 -> 0
0.51 -> 1
and so on for positive values
-0.49 -> 0
-0.50 -> 0
-0.51 -> -1
and so on for negative values.

Thanks in advance.

Luigi

Math.Round(d, MidpointRounding.ToEven) gives you that result.
 
Math.Round(d, MidpointRounding.ToEven) gives you that result.

I was wondering why he didn't use that but all I got was a note that
it doesn't work.

I have tried it and it does exactly what he was asking so I don't know
if there is some other difference that I don't understand.

Tom P.
 
Tom said:
I was wondering why he didn't use that but all I got was a note that
it doesn't work.

I have tried it and it does exactly what he was asking so I don't know
if there is some other difference that I don't understand.

Tom P.

I guess that he might have some other requiremnents that he hasn't
mentioned. For example whether 1.50 should be rounded to 1 or 2...
 
Göran Andersson said:
I guess that he might have some other requiremnents that he hasn't
mentioned. For example whether 1.50 should be rounded to 1 or 2...


My guess was he wanted TowardZero, not AwayFromZero.
 
Back
Top