Problem In OutPut Of Divided Number [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Divide problem
Why does c# show the output of this code as 0??
MessageBox.Show((5/6).ToString);

Because unless you specify that you want the operation to result in a Double the operation results in an Integer, and so the fractional result is dropped and you are left with just the whole number of 0.

5/6 is basically integral division, which turns out to be 0. The type of both operands is int.
I think what you want is : 5.0/6.0.
In fact, 5.0/6.0, 5/6.0, 5.0/6, all would give same result. That is, as long as, one operand is double, it would be a double division, and the type of the result would be double as well.

It is dividing an integer by and integer and will return an integer, I believe it always returns the floor value. Try Messagebox.Show((5.0/6.0).ToString());

Because you are doing integer division. If you want non-integer division you should do something like 5 / 6d

The compiler assumes that the numbers are Int, which must be whole numbers. Thus, it is rounding the answer. To return the decimal answer, use this:
MessageBox.Show((5d/6d).ToString());

Related

Math.Sqrt(2/3) returns 0 [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 6 years ago.
I am really confused why this is happening, my code:
double x = Math.Sqrt(2/3);
MessageBox.Show(x.ToString());
Displays 0.
The answer is
0.8164, I know I will also have to use Math.Round to round this up, but for the moment the issue is I'm getting 0
The problem is caused by automatic integer evaluation of the numbers. Use:
double x = Math.Sqrt(2f/3f);
MessageBox.Show(x.ToString());
2 / 3 is an integer operation, what you want is 2.0 / 3 which means I want to use floating point numbers.
What you consider an Intereger is different from what you know from Maths. In programming languages it means that a result of an int-operation is allways an integer in itself.
In your example 2 / 3 is an integer-operation which means the result is rounded down to the nearest integer, which is zero. To avoid this indicate that at least one of your operands should be treates as some floating-point value, either using 2.0 or 2f (alternativly 3.0 or 3f).

Why Math.Round() works differently in C# [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
First I noticed that Math.Round() doesn't round for 2 digits as I as for:
double dd = Math.Round(1.66666, 2);
Result:
dd = 1.6699999570846558;
and then I created a new project with same .NET framework and the result is 1.67 now as it should been in the first place.
I've never seen Round behaving like this before, what is causing this problem?
Like the other comments mentioned, use decimal to hold the returned value:
decimal dd = Math.Round(1.66666M, 2);
The issue you described has nothing to do with the Round() function. You can read a bit about how the floating point numbers & fixed point numbers work, but the short explanation is:
With floating point variables (e.g. double), you cannot guarantee the precision of the number you save it them. So when you save something like 1.67 in a variable of the type double and then you check the value later on, therer is no guarantee you will get exactly 1.67. You may get a value like 1.66999999 (similar to what you got) or like 1.6700000001.
Fixed point variables (e.g. decimal) on the other hand, will give you that precision, so if you save 1.67, you will always get 1.67 back.
Please note that the Round() function returns the same type that you pass to it, so to make return a decimal, you need to pass it 1.66666M which is a decimal value rather than 1.66666 which is a floating point number.

float.Parse returns incorrect value [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 6 years ago.
I have a string X with value 2.26
when I parse it using float.Parse(X) ..it returns 2.2599999904632568. Why so? And how to overcome this ?
But if instead I use double.Parse(X) it returns the exact value, i.e. 2.26.
EDIT: Code
float.Parse(dgvItemSelection[Quantity.Index, e.RowIndex].Value.ToString());
Thanks for help
This is due to limitations in the precision of floating point numbers. They can't represent infinitely precise values and often resort to approximate values. If you need highly precise numbers you should be using Decimal instead.
There is a considerable amount of literature on this subject that you should take a look at. My favorite resource is the following
What Every Computer Scientist Should Know About Floating Point
Because floats don't properly represent decimal values in base 10.
Use a Decimal instead if you want an exact representation.
Jon Skeet on this topic
Not all numbers can be repesented exactly in floating point. Approximations are made and when you have operation after operation on an unexact number the situation gets worse.
See this Wikipedia entry for an example: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
If you changed you inputs to something that can be represented exactly by floating point (like 1/8), it would work. Try the number 2.25 and it will work as expected.
The only numbers that will work exactly are numbers that can be represented by the sum of any of 1/2, 1/4, 1/8, 1/16, etc since those numbers are represented by the binary 0.1, 0.01, 0.001, 0.0001, etc.
This situation happens with all floating point systems, by nature. .Net, JavaScript, etc.
It is returning the best approximation to 2.26 that is possible in a float. You're probably getting more significant digits than that because your float is being printed as a double instead of a float.
When I test
var str = "2.26";
var flt = float.Parse(str);
flt is exactly 2.26 in the VS debugger.
How do you see what it returns?

Problem with Double and division [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Ints and Doubles doing division
1/252 = 0 in c#?
Hi,
Maybe because it's Friday, but I cannot understand this:
(Double)1/2 = 0.5
(Double)1/(Double)2 = 0.5
(Double)((Double)1/(Double)2) = 0.5
(Double)(1/2) = 0.0
Why the last operation is 0? :S
Kind regards.
Because 1 and 2 are integers. The result is 0. If you cast that to a double, it's still 0. This question was asked just a couple of days ago.
If you divide two ints, then the result will also be int. So 1/2 gives you a zero which is an integer. Then you are casting 0 to double, which is still zero.
Everyone's given you the correct answer so far, I'm adding this so other readers don't miss it in the comments.
Use the same rule as regular math. Inner Parenthesis first. So in the first example, the 1 is casted to a double before the division occurs, making the result a double (division of int and double results in double). This rings true if it is (Double)1/2 or 1/(Double)2. So in the last example, (Double)(1/2), the (1/2) is performed first, int on int, resulting in int. Then the (Double) casts it to a Double. Hope this not only helps you but anyone else curious about this question. I myself have had many times where I had a long equation and literally had to cast each parameter of the equation to a double.
Try (double)(1.0/2.0) - that will give the answer you expect.
As already written, the problem is the type. You can use suffixes to make sure the type is correct:
1d/2d=0.5
(Double)1/2 = 0.5
(Double)1/(Double)2 = 0.5
(Double)((Double)1/(Double)2) = 0.5
(Double)(1/2) = 0.0
In first three cases You cast the integer value (thas is default type for number when you do not use suffix or does not contain dot ) to Double and then do the division with an integer value in first case and with double for 2 and 3, in contrast to last (4) case where the brackets change the order of operation first You divide two integers, and after that cast the result to Double.

round decimal values up to the nearest of 0.01? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
How to round decimal value up to nearest 0.05 value??, the linked SO post also discusses the similar topic, but its not the output i expected.
I need to convert the decimal values like this
16.489-->16.49
16.482-->16.48
16.425-->16.43
7.67 --> 7.67 (no conversion)
I can use the below C# method to convert the values
Math.Round(16.482*20)/20;
But this method not works for me, it gives the following results
16.489-->16.5
16.482-->16.5
7.67 --> 7.7
16.425-->16.45
whats the elegant way in c# to do this.
Math..::.Round Method (Decimal, Int32, MidpointRounding)
Rounds a double-precision floating-point value to the specified number of fractional digits. A parameter specifies how to round the value if it is midway between two other numbers.
Math.Round(1.489,2,MidpointRounding.AwayFromZero)
Did you try
Math.Round(16.482*200)/200;

Categories

Resources