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 13 4335
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
How about something cheeky like:
decimal a = Math.Ceiling(x0.5M);
Obviously you'd need to think about ves etc...
Marc
On Jul 1, 10:23*am, Luigi <ciupazNoSpamGra...@inwind.itwrote:
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)
Assuming you mean 10 raisedtothepowerof (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
On Tue, 1 Jul 2008 07:55:38 0700 (PDT), "Jon Skeet [C# MVP]"
<sk***@pobox.comwrote:
>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);
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 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
Thank you all very much.
I'll try this one method:
decimal x = 1.51m;
decimal a = Math.Ceiling(x  0.5M);
Luigi
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
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" <ma**********@gmail.comwrote in message
news:c1**********************************@56g2000h sm.googlegroups.com...
>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
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
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
"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(x0.5M);
but not works correctly.
1.6 1 (and not 2)
L
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" <ci****************@inwind.itwrote in message
news:F9**********************************@microsof t.com...
"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(x0.5M);
but not works correctly.
1.6 1 (and not 2)
L
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Batista, Facundo 
last post by:
Here I send it.
Suggestions and all kinds of recomendations are more than welcomed.
If it all goes ok, it'll be a PEP when I finish writing/modifying the code.
Thank you.
.. Facundo

by: John Bentley 
last post by:
John Bentley:
INTRO
The phrase "decimal number" within a programming context is ambiguous. It could
refer to the decimal datatype or the related but separate concept of a generic
decimal number....

by: cody 
last post by:
no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.
i heard people say C++ is slower than C but i can't...

by: Ron 
last post by:
Greetings,
int i = 1, j = 6;
double k = (double)i/j;
Console.WriteLine(k.ToString());
Console.WriteLine(string.Format(k.ToString(), "0.00"));
both yield 0.166666666666667
how can I make...

by: vooose 
last post by:
Consider a rounding up function:
public static decimal RoundUp(decimal val, decimal round)
{
return ((decimal)Math.Ceiling((double)(val/round)))*round;
}
Math.Ceiling (and Math.Floor for...

by: spebola 
last post by:
I am using vb.net 2003 professional and I get the following results
when using the round method:
dim Amount as decimal = 180.255
Amount = Amount.Round(Amount, 2)
Amount now contains 180.25. ...

by: =?Utf8?B?RXRoYW4gU3RyYXVzcw==?= 
last post by:
Hi,
Why does Math.Sqrt() only accept a double as a parameter? I would think
it would be just as happy with a decimal (or int, or float, or ....). I can
easily convert back and forth, but I am...

by: =?Utf8?B?THVpZ2k=?= 
last post by:
Hi all,
I have a collection (an array for example) of decimal values.
I need to round them and take care of the roundings.
For example, if I have
decimal a = 10.50m
should became 10
and if

by: Jason 
last post by:
I'm making a program that will convert decimal inputs (in this case, in
inches) and output a fractional answer. At the moment, I'm only able to
output the fractional answer in three parts: A whole...

by: isladogs 
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...

by: veera ravala 
last post by:
ServiceNow is a powerful cloudbased platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...

by: jianzs 
last post by:
Introduction
Cloudnative applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...

by: abbasky 
last post by:
### Vandf component communication method one: data sharing
Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...

by: jimatqsi 
last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...

by: stefan129 
last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multidomain SSL certificates? Any recommendations on reliable providers or specific...

by: egorbl4 
last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это
Что это? Что мне с этим делать?
...

by: davi5007 
last post by:
Hi,
Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

by: MeoLessi9 
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
 