Connecting Tech Pros Worldwide Help | Site Map

Rounding decimal

=?Utf-8?B?THVpZ2k=?=
Guest
 
Posts: n/a
#1: Nov 6 '08
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.
--
Luigi
http://blogs.dotnethell.it/ciupaz/
Hans Kesting
Guest
 
Posts: n/a
#2: Nov 6 '08

re: Rounding decimal


After serious thinking Luigi wrote :
Quote:
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


=?Utf-8?B?THVpZ2k=?=
Guest
 
Posts: n/a
#3: Nov 6 '08

re: Rounding decimal


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

-1.5 = - 1

- 1.6 = - 2

-1.4 = - 1


L
=?Utf-8?B?THVpZ2k=?=
Guest
 
Posts: n/a
#4: Nov 6 '08

re: Rounding decimal


"Hans Kesting" wrote:
Quote:
After serious thinking Luigi wrote :
Quote:
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.
Ok Hans, I'll checking.

Luigi
Closed Thread