Rounding decimal

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.
 
H

Hans Kesting

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
 
L

Luigi

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

-1.5 = - 1

- 1.6 = - 2

-1.4 = - 1


L
 
L

Luigi

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top