Math.Round() seems to be not consistent [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
.Net Round Bug
In C#: Math.Round(2.5) result is 2 (instead of 3)! Are you kidding me?
Code:
var d1 = Math.Round(187.5); // 188
var d2 = Math.Round(62.5); // 62
Why is it so?

By default, Math.Round uses a form of rounding called Banker's Rounding, which rounds to the nearest even integer when the input is halfway between two integers.
See Why does .NET use banker's rounding as default? for an understanding of this design decision.
If you don't like this behaviour, you can always use this overload of Math.Round, which lets you specify the MidPointRoundingMode (ToEven, AwayFromZero).

You can change this behaviour with an call to this overload of the method - http://msdn.microsoft.com/en-us/library/ms131274.aspx

Related

Correct way to precisely round decimals in C# [duplicate]

This question already has answers here:
How do I make it round off instead of always round down?
(2 answers)
How to round a decimal up?
(6 answers)
Closed 5 months ago.
This is my code:
decimal test1 = 190.5m;
decimal test2 = 191.5m;
var testResult1 = Math.Round(test1, 0); // =190 It should be 191!!!!
var testResult2 = Math.Round(test2, 0); // =192 Correct.
Is there a way I could get the correct result? Maybe another library instead of System.Math?
This is by design. Albeit not what we have grown up with.
Find how you manage to round the way you expect here:
https://learn.microsoft.com/en-us/dotnet/api/system.midpointrounding?redirectedfrom=MSDN&view=net-6.0
For a bit more reading and background, have a browse through some of the answers here:
Why does .NET use banker's rounding as default?

Round off decimal value in Nearest 10 or Zero [duplicate]

This question already has answers here:
C# - Math.Round
(5 answers)
Closed 4 years ago.
Following values i would like to convert to round off figure. likes:
60.72 --> 60.70
170.76 --> 170.80
Currently, I'm converted to round off value using below method:
getFee.ServiceRequestFee.ToString("N")
I'm not sure which Match.Round method suitable to my requirement.
Edit:
protected string Getroundoffdecimalvalue(string servicerequestsfee_val)
{
servicerequestsfee_val = Math.Round(Convert.ToDecimal(servicerequestsfee_val), 2).ToString();
return servicerequestsfee_val;
}
I used this function even after i return 60.72 only and my expectation should 60.70.
Math.Round(yourNumber, 1)
The second parameter is number of decimal places to round to. In your case you want 1 decimal place as an end result.
**
You need to overloadMath.round that takes the decimals parameter of
your choice and convenience.
**
Use Math.round and if needed convert the same to string.
Math.Round(var_name,2)

Inconsistent Math.Floor results [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 4 years ago.
I'm a bit puzzled by the results of the Math.Floor function in C#.
I get a return of 91 as expected with the call below:
Math.Floor(91.0);
But if I use the call below I get a returned value of 90, while I still expect 91 in this case.
Math.Floor(9.1/0.1);
Is this just due to small rounding errors and is there a way to get consistent results?
Yes, this problem is related to precision, since Math.Floor() simply doesn't round mathematically and always down instead. So even 90.9999999 still results in only 90.
For accurate rounding, use Math.Round() instead or add 0.5 to the value passed.

Math.Abs weird behaviour [duplicate]

This question already has answers here:
Double vs Decimal Rounding in C#
(2 answers)
Closed 7 years ago.
I am doing following equation but the result is not as expected
double dasdas = Math.Abs(3.2 - 1.9);
The result is
1.3000000000000003
However the correct result should be
1.3
What may be the reason of this?
c# 4.5.2
This is because you're using a double - which is a floating point. These by definition this cannot store the exact number.
You need to use Decimal.
Take at look at here and What Every Computer Scientist Should Read About Floating Point
Use decimal:
decimal dasdas = Math.Abs(3.2m - 1.9m);

Math.Round(((4.006+4.007)/2),3) = 4.006? [duplicate]

This question already has answers here:
C# - (int)Math.Round((double)(3514 + 3515)/2) =3514?
(2 answers)
Closed 9 years ago.
Similar question, but without double and 3 decimal places.
The difference is that the average of two integers we may have a double as a result, but when we use (int) Math.Ceiling ((double) value), result an integer.
C# - (int)Math.Round((double)(3514 + 3515)/2) =3514?
But in this case, we have two doubles and
Math.Round(((4.006+4.007)/2),3); // returns 4.006
Math.Round(((4.008+4.007)/2),3); // returns 4.008
WHY?
From the MSDN:
Return Value
Type: System.Double The integer nearest a. If the fractional component
of a is halfway between two integers, one of which is even and the
other odd, then the even number is returned. Note that this method
returns a Double instead of an integral type.
Remarks
The behavior of this method follows IEEE Standard 754, section 4. This
kind of rounding is sometimes called rounding to nearest, or banker's
rounding. It minimizes rounding errors that result from consistently
rounding a midpoint value in a single direction.
Also check this related thread

Categories