On Jul 1, 3:23*pm, Luigi <ciupazNoSpamGra...@inwind.itwrote:

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