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
Related
This question already has answers here:
Truncate Two decimal places without rounding
(24 answers)
Closed 4 years ago.
For example if I divide 1050 / 256 I get 4.1015625. I need the value of first digit after decimal point (1 in this case). I don't want to involve ToString() conversions and then parsing it into digit again.
This picture for itsme86:
This picture for Jeroen Mostert:
What about:
decimal result = 4.1015625m;
result = result - (int)result;
result = Decimal.Round(result, 1, MidpointRounding.AwayFromZero);
This can be combined into one line if needed but is more readable this way.
Math.Floor((4.1015625 - Math.Floor(4.1015625)) * 10)
I would do it this way:
First i would subtract the the number before the decimal seperator of the given number.
Then I would multiply it by 10.
Example: 4.2 - 4 = .2 * 10 = 2
NOTE: You cannot use floating point numbers for accurate mathematical operations, as some values cannot be represented properly. So always cast to/use decimal and not float/double if you need exact values like this.
This question already has answers here:
c# string to float conversion invalid?
(3 answers)
Closed 5 years ago.
float.Parse("534818068")
returns: 534818080
I understand that there are many complications with float and decimal values. But maybe someone could explain this behaviour to me.
Thanks!
Floating point numbers have a relative precision, i.e. something like 7 or 8 digits. So only the first 7 or 8 digits are correct, independent of the actual total size of the number.
Floating point numbers are stored internally using the IEEE 754 standard (a sign, a biased exponent and a fraction).
float numbers are stored with a 32 bits representation, which means they will have a precision of 7 digits.
On the other hand, double are stored with a 64 bits representation, thus having 15-16 digits (source).
Which is why you shouldn't usually compare floats for equality for instance.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
AFAIK .NET's default round option is to even, so Math.Round(1.225,2) should give 1.22 but it gives 1.23.
Math.Round(2.225,2) = 2.22
Math.Round(100.225,2) = 100.22
all the values I tried rounds to nearest even but only 1.225 and -1.225 rounds to 1.23 and -1.23.
The main problem is that in float and double, the amount of decimal places is not part of the value, and the precision isn't decimal, but rather, binary. And there's no finite binary number that can represent 1.225 exactly.
So when you do Math.Round(1.225f, 2), you're actually doing something more like Math.Round(1.22500002f, 2) - there's no midpoint rounding involved.
The same problem appears with Math.Round(2.225f, 2) - it's just that the "real" value is slightly smaller than 2.225f, so the result rounds down. But there's still no midpoint rounding involved.
If you need decimal precision, use decimal. Neither float nor double are designed for decimal precision - they're fine for e.g. physics calculations, but not for e.g. accounting.
1.225 can't be represented exactly in floating point, so you're really rounding 1.225000023841858.
This question already has answers here:
Implicitly converting int to double
(8 answers)
Closed 7 years ago.
May be I'm asking silly question but I don't understand why this doesn't give me the output I expect (i.e 2.5):
double x = 5/2;
Console.WriteLine(x.ToString());
.Net Fiddle
5 / 2 performs integer division no matter which type you assign it. It always disregards fractional part.
You need to use floating-point division instead.
double x = 5.0 / 2;
double x = 5 / 2.0;
double x = 5.0 / 2.0;
From / Operator
When you divide two integers, the result is always an integer. For
example, the result of 7 / 3 is 2.
From C# Specification part $7.7.2 Division operator, there 3 types of division;
Integer division
Floating-point division
Decimal division
And from the relevant part in integer division;
The division rounds the result towards zero, and the absolute value of
the result is the largest possible integer that is less than the
absolute value of the quotient of the two operands. The result is zero
or positive when the two operands have the same sign and zero or
negative when the two operands have opposite signs.
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