Rounding decimal

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

Luigi

Hi all,
I have to write a snippet that performs a rounding of decimal value, follow
this rules:

1.49m -> 1m
1.5 -> 2m
1.51 -> 2m

-1.49 -> -1m
-1.50 -> -2

I was trying this:

Math.Ceiling((-voceValue.Value ?? 0m) - 0.5M);

but not works correctly.

Has anyone idea?

Thanks a lot.
 
After serious thinking Luigi wrote :
Hi all,
I have to write a snippet that performs a rounding of decimal value, follow
this rules:

1.49m -> 1m
1.5 -> 2m
1.51 -> 2m

-1.49 -> -1m
-1.50 -> -2

I was trying this:

Math.Ceiling((-voceValue.Value ?? 0m) - 0.5M);

but not works correctly.

Has anyone idea?

Thanks a lot.

Math.Round has an overload that accepts a MidpointRounding value. You
can set that to "ToEven" or "AwayFromZero". I guess you need the second
value.

Hans Kesting
 
A small correction for negative values.
These are the rules:

-1.5 = - 1

- 1.6 = - 2

-1.4 = - 1


L
 
Hans Kesting said:
After serious thinking Luigi wrote :

Math.Round has an overload that accepts a MidpointRounding value. You
can set that to "ToEven" or "AwayFromZero". I guess you need the second
value.

Ok Hans, I'll checking.

Luigi
 
Back
Top