PC Review


Reply
Thread Tools Rate Thread

Custom decimal round

 
 
Luigi
Guest
Posts: n/a
 
      1st Jul 2008
Hi all,
I need to round a decimal value with a particular rule.
For example:

decimal a = 1.49m -> 1m
decimal a = 1.5m -> 1m
decimal a = 1.51m -> 2m

I've tried Math.Round but with the value 1.5 it returns me 2, which it is
not correct for my business rule.

How can I solve this problem?

Thanks in advance.


--
Luigi

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      1st Jul 2008
How about something cheeky like:

decimal a = Math.Ceiling(x-0.5M);

Obviously you'd need to think about -ves etc...

Marc
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st Jul 2008
On Jul 1, 3:23*pm, Luigi <ciupazNoSpamGra...@inwind.it> wrote:
> I need to round a decimal value with a particular rule.
> For example:
>
> decimal a = 1.49m -> 1m
> decimal a = 1.5m -> 1m
> decimal a = 1.51m -> 2m
>
> I've tried Math.Round but with the value 1.5 it returns me 2, which it is
> not correct for my business rule.
>
> How can I solve this problem?


I'd write a custom rounding method. It's reasonable simple if you have
a very specific requirement. In this case, something like:

public decimal Round (decimal value)
{
decimal floor = Math.Floor(value);
decimal ceiling = Math.Floor(value);
decimal midpoint = (floor+ceiling)/2;
return value <= midpoint ? floor : ceiling;
}

It's possible that there will be some weird problems around the very
largest numbers that decimals can store, but if your application
doesn't use those (and it's unlikely to) then you should be okay.

This is completely untested though - I strongly recommend writing unit
tests for it!

Jon
 
Reply With Quote
 
SLL
Guest
Posts: n/a
 
      1st Jul 2008
On Jul 1, 10:23*am, Luigi <ciupazNoSpamGra...@inwind.it> wrote:
> Hi all,
> I need to round a decimal value with a particular rule.
> For example:
>
> decimal a = 1.49m -> 1m
> decimal a = 1.5m -> 1m
> decimal a = 1.51m -> 2m
>
> I've tried Math.Round but with the value 1.5 it returns me 2, which it is
> not correct for my business rule.
>
> How can I solve this problem?
>
> Thanks in advance.
>
> --
> Luigi


Try multiplying by 10*(number of decimals needed), used math.floor or
math.ceiling, then divide by same number 10*(number of decimals
needed)

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      1st Jul 2008

Assuming you mean 10 raised-to-the-power-of (number of decimals), the OP
appears to want zero decimals. Taking the cited example 1.51, this would
give 1M, not 2M as desired.

Marc
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st Jul 2008
rossum <(E-Mail Removed)> wrote:

<snip>

> > decimal ceiling = Math.Floor(value);

> Did you mean "decimal ceiling = Math.Ceiling(value);"? rossum


Yup. That's what I get for not testing it...

--
Jon Skeet - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
Luigi
Guest
Posts: n/a
 
      2nd Jul 2008
Thank you all very much.
I'll try this one method:

decimal x = 1.51m;

decimal a = Math.Ceiling(x - 0.5M);

Luigi
 
Reply With Quote
 
Michel Walsh
Guest
Posts: n/a
 
      2nd Jul 2008
Note that this is called the banker rounding: if, for x a positive integer,
x + .5 is always rounded to (x+1), the banker ends up loosing money. So,
1.5 is rounded to 2, that is, upward, the banker lost is 0.5; while 2.5 is
*also* rounded to 2, that is, downward, and the banker get a profit of 0.5
in that case.

Statistically, the banker should be not loosing, neither winning (assuming
the integer part has no bias toward odd or toward even numbers) with that
way to round. You probably already know it, but just in case someone reading
this thread was not.

Vanderghast, Access MVP

"Luigi" <(E-Mail Removed)> wrote in message
news:F676C879-12A5-4B4E-B8D5-(E-Mail Removed)...
> Hi all,
> I need to round a decimal value with a particular rule.
> For example:
>
> decimal a = 1.49m -> 1m
> decimal a = 1.5m -> 1m
> decimal a = 1.51m -> 2m
>
> I've tried Math.Round but with the value 1.5 it returns me 2, which it is
> not correct for my business rule.
>
> How can I solve this problem?
>
> Thanks in advance.
>
>
> --
> Luigi
>


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      3rd Jul 2008
> Note that this is called the banker rounding

I'm fairly certain that the original question as posed doesn't relate
to banker's rounding; it is just .5 always rounding down.

Marc
 
Reply With Quote
 
Michel Walsh
Guest
Posts: n/a
 
      3rd Jul 2008
Yes, sorry, I was not clear. The OP want a rounding down at 0.5, but GOT a
banker rounding, with Math.Round(x, 0).

Vanderghast, Access MVP


"Marc Gravell" <(E-Mail Removed)> wrote in message
news:c1e60763-827e-44a7-9cfc-(E-Mail Removed)...
>> Note that this is called the banker rounding

>
> I'm fairly certain that the original question as posed doesn't relate
> to banker's rounding; it is just .5 always rounding down.
>
> Marc



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Round Up a Decimal jlo Microsoft Access 6 27th Jul 2009 06:14 PM
Round down if decimal 0.33 or below up if 0.34 or above tennisnut Microsoft Excel Misc 3 23rd Apr 2009 11:57 AM
Decimal round Luigi Z Microsoft C# .NET 7 26th Jan 2009 10:24 AM
how do i round decimal if 2.3=2.5 2.7=2.5 & 2.8=3.0 tiffa Microsoft Excel Worksheet Functions 4 5th May 2008 02:00 PM
decimal to round off =?Utf-8?B?R2VyYWxk?= Microsoft Access Form Coding 14 12th Jun 2006 05:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:58 AM.