455,566 Members | 1,774 Online Need help? Post your question and get tips & solutions from a community of 455,566 IT Pros & Developers. It's quick & easy.

# Custom decimal round

 P: n/a 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 Jul 1 '08 #1
13 Replies

 P: n/a On Jul 1, 3:23*pm, Luigi

 P: n/a How about something cheeky like: decimal a = Math.Ceiling(x-0.5M); Obviously you'd need to think about -ves etc... Marc Jul 1 '08 #3

 P: n/a On Jul 1, 10:23*am, Luigi

 P: n/a 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 Jul 1 '08 #5

 P: n/a On Tue, 1 Jul 2008 07:55:38 -0700 (PDT), "Jon Skeet [C# MVP]" On Jul 1, 3:23*pm, Luigi I need to round a decimal value with a particular rule.For example:decimal a = 1.49m -1mdecimal a = 1.5m -1mdecimal a = 1.51m -2mI've tried Math.Round but with the value 1.5 it returns me 2, which it isnot correct for my business rule.How can I solve this problem? I'd write a custom rounding method. It's reasonable simple if you havea very specific requirement. In this case, something like:public decimal Round (decimal value){ decimal floor = Math.Floor(value); decimal ceiling = Math.Floor(value); Did you mean "decimal ceiling = Math.Ceiling(value);"? rossum decimal midpoint = (floor+ceiling)/2; return value <= midpoint ? floor : ceiling;}It's possible that there will be some weird problems around the verylargest numbers that decimals can store, but if your applicationdoesn't use those (and it's unlikely to) then you should be okay.This is completely untested though - I strongly recommend writing unittests for it!Jon Jul 1 '08 #6

 P: n/a rossum 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 - Web site: http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon_skeet C# in Depth: http://csharpindepth.com Jul 1 '08 #7

 P: n/a Thank you all very much. I'll try this one method: decimal x = 1.51m; decimal a = Math.Ceiling(x - 0.5M); Luigi Jul 2 '08 #8

 P: n/a 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 Jul 3 '08 #9

 P: n/a 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" 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 Jul 3 '08 #10

 P: n/a Another question. To adhere to these rules when the values are negative: Criteria to round negative amounts are the following: -1.5 =- 1 - 1.6 =- 2 -1.4 =- 1 how can I write a method that make this rounding? Values are decimal. Thanks. Luigi Jul 15 '08 #11

 P: n/a If this follows the same rules as +ve, but negated - then perhaps just check whether the value is <0, and then: * negate the value (making it +ve) * apply the current rounding logic (which works for +ve) * negate the result (making it -ve) Marc Jul 15 '08 #12

 P: n/a "Marc Gravell" wrote: If this follows the same rules as +ve, but negated - then perhaps just check whether the value is <0, and then: * negate the value (making it +ve) * apply the current rounding logic (which works for +ve) * negate the result (making it -ve) I'm using: decimal a = Math.Ceiling(x-0.5M); but not works correctly. -1.6 --1 (and not -2) L Jul 15 '08 #13

 P: n/a The ceiling is the first integer greater or equal to its argument. Technically, -1 is greater than (is to the 'right' of, if you draw the x axis) -1.6. -2 is smaller, and to the left, of -1,.6. As suggested by Marc Gravel, you can use: y = ( x>=0) ? roundingPositiveValue(x) : - roundingPositiveValue( -x ) ; where 'roudingPositiveValue' is your required method. Vanderghast, Access MVP "Luigi" If this follows the same rules as +ve, but negated - then perhaps justcheck whether the value is <0, and then:* negate the value (making it +ve)* apply the current rounding logic (which works for +ve)* negate the result (making it -ve) I'm using: decimal a = Math.Ceiling(x-0.5M); but not works correctly. -1.6 --1 (and not -2) L Jul 15 '08 #14

### This discussion thread is closed

Replies have been disabled for this discussion. 