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 4388
On Jul 1, 3:23*pm, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
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(valu e);
decimal ceiling = Math.Floor(valu e);
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(x-0.5M);
Obviously you'd need to think about -ves etc...
Marc
On Jul 1, 10:23*am, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
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 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
On Tue, 1 Jul 2008 07:55:38 -0700 (PDT), "Jon Skeet [C# MVP]"
<sk***@pobox.co mwrote:
>On Jul 1, 3:23*pm, Luigi <ciupazNoSpamGr a...@inwind.itw rote:
>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(valu e);
decimal ceiling = Math.Floor(valu e);
Did you mean "decimal ceiling = Math.Ceiling(va lue);"? 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**********@g mail.comwrote in message
news:c1******** *************** ***********@56g 2000hsm.googleg roups.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
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. "Decimal Number" sometimes serves to distinguish Base 10
numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal
Number" serves to...
|
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 believe that. in pieces
of the application where speed really matters you can still use "normal"
functions or even static methods which is...
|
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 it yield 0.17? I know there is some
|
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 RoundDown) only take double,s so we
need to cast twice. Is there a better way?
| |
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. I need it to contain 180.26.
Any ideas?
|
by: =?Utf-8?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 interested in what is going on behind
the scenes and if there is some aspect of decimals that keep them from being
used in this calculation.
Thanks!...
|
by: =?Utf-8?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 number text box, a
numerator text box, and a denominator text box. I realize that this is
probably the closest I'll get to having fractions...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |