This has a potentially simple answer but I can't figure it out -
double Result = 1 / 12;
returns 0, while
double Result2 = 24 / 12;
return 2
What's going on and how can I fix it?
Try this:
double Result = 1 / (double)12;
or this:
double Result = 1 / 12D;
In C# (and also in a lot of other languages), integer division returns an integer. By casting one of the operands to double or explicitly declaring a literal double you can force the division expression to return a double and not truncate after the decimal place.
it is doing integer math because the numbers on the right are evaluated as integers.
try 1.0/12;
this will work too
Decimal.Divide(1, 12)
It has a result with higher precision, but a smaller range.
The problem is that 1 and 12 are integers (of type int, not double). This means the values ignore anything past the decimal point. When you divide 1 by 12, you get 0.083. Since anything past the decimal point is truncated for int, you are left with 0.
To get expected results, one of your operands needs to be of type double. You can do this by changing 1 to 1.0 or 12 to 12.0 (or both, as long as at least one of the operands is a double).
I think you need to cast your values
double Result = (double)1 / (double)12
has something to do with integer based math always returns an integer.....
Related
When I make a division in C#, it automaticaly rounds down. See this example:
double i;
i = 200 / 3;
Messagebox.Show(i.ToString());
This shows me a messagebox containing "66". 200 / 3 is actually 66.66666~ however.
Is there a way I can avoid this rounding down and keep a number like 66.6666667?
i = 200 / 3 is performing integer division.
Try either:
i = (double)200 / 3
or
i = 200.0 / 3
or
i = 200d / 3
Declaring one of the constants as a double will cause the double division operator to be used.
200/3 is integer division, resulting in an integer.
try 200.0/3.0
200 / 3 this is an integer division. Change to: 200.0 / 3 to make it a floating point division.
You can specify format string with the desired number of decimal ponits:
double i;
i = 200 / 3.0;
Messagebox.Show(i.ToString("F6"));
Though the answer is actually 66.666, what is happening is that 200 / 3 is being calculated resulting in an integer. The integer is then being placed in the float. The math itself is happening as integer math. To make it a float, use 200.0 / 3. The .0 will cause it to treat 200 as a float, resulting in floating point math.
Aside from the double vs int happening in that action, you're thinking of double as a precise unit. Try using the decimal datatype when you really care about accuracy.
More information at this answer:
decimal vs double! - Which one should I use and when?
double i = 200.0 / 3;
double i = ((double)200)/3;
What happens is the two integers perform an integer divide, and then the integer answer is assigned to the float. To avoid that, always cast one of the numbers as a double.
Try this
i = 200d/3d;
and it will not round.
200 and 3 are both integers, so the result will be an integer. Convert one of them to a decimal.
All given answers are wrong because they translate the integer division into one of kind double, which is cleanly not what was asked for (at least from a performance standpoint). The obvious answer is elementary school math, multiply by 10, add 5 and divide again, all integer.
i = (2000 / 3 + 5 ) / 10
You are catching a second division here, which is better than doing double conversions but still far from perfect. You could go even further and multiply by another factor and add other values than five, thus allowing you to use right shifting instead of dividing by 10. The exact formula for doing this is left as an exercise to the reader. (Just google "divisions with Multiply Shift")
Have a nice day.
I've got this line of code:
int WStoneCost = PriceMethod.StoneCost / 100 * AP;
While PriceMethod.StoneCost is equal to 25 and AP is equal to 70.
I've checked it using breakpoints and I can't understand why do I get zero after I run this line. (WStoneCost is equal to zero)
Is it just a wrong symbol or am I doing something wrong? Thanks in advance.
And how to get a correct double at the end? Like 17.5
You are doing integer division, so 25/100 is 0, not 0.25, and hence 0 * 70 is 0. Since your result variable is also an int it's unclear what result you are expecting, but you could reorder the operations to get a non-zero answer:
int WStoneCost = (PriceMethod.StoneCost * AP)/ 100 ;
It's still integer division, but with your inputs will divide 25*70 (1,750) by 100, which will give you 17.
If you want a floating-point decimal result, just use 100m:
decimal WStoneCost = (PriceMethod.StoneCost * AP)/ 100m ;
Since the literal 100m is a decimal, then the compiler will use floating-point decimal division, which will give you a decimal result.
And how to get a correct double at the end? Like 17.5
Your question and both of the two answers given so far indicate that all three of you want to do something dangerously wrong. You are doing financial calculations so you should always be using decimal, never double. double is for physics calculations, not financial calculations.
I agree with JonathanWood. According to the values that you provided, your answer is going to produce a fractional number. Therefore, you need WStoneCost to be a double or a float. Also, you might want to use partentheses in your equations to ensure that the order of operations is carried out to your expectations.
Hope this helps!
-Gary The Bard
When I make a division in C#, it automaticaly rounds down. See this example:
double i;
i = 200 / 3;
Messagebox.Show(i.ToString());
This shows me a messagebox containing "66". 200 / 3 is actually 66.66666~ however.
Is there a way I can avoid this rounding down and keep a number like 66.6666667?
i = 200 / 3 is performing integer division.
Try either:
i = (double)200 / 3
or
i = 200.0 / 3
or
i = 200d / 3
Declaring one of the constants as a double will cause the double division operator to be used.
200/3 is integer division, resulting in an integer.
try 200.0/3.0
200 / 3 this is an integer division. Change to: 200.0 / 3 to make it a floating point division.
You can specify format string with the desired number of decimal ponits:
double i;
i = 200 / 3.0;
Messagebox.Show(i.ToString("F6"));
Though the answer is actually 66.666, what is happening is that 200 / 3 is being calculated resulting in an integer. The integer is then being placed in the float. The math itself is happening as integer math. To make it a float, use 200.0 / 3. The .0 will cause it to treat 200 as a float, resulting in floating point math.
Aside from the double vs int happening in that action, you're thinking of double as a precise unit. Try using the decimal datatype when you really care about accuracy.
More information at this answer:
decimal vs double! - Which one should I use and when?
double i = 200.0 / 3;
double i = ((double)200)/3;
What happens is the two integers perform an integer divide, and then the integer answer is assigned to the float. To avoid that, always cast one of the numbers as a double.
Try this
i = 200d/3d;
and it will not round.
200 and 3 are both integers, so the result will be an integer. Convert one of them to a decimal.
All given answers are wrong because they translate the integer division into one of kind double, which is cleanly not what was asked for (at least from a performance standpoint). The obvious answer is elementary school math, multiply by 10, add 5 and divide again, all integer.
i = (2000 / 3 + 5 ) / 10
You are catching a second division here, which is better than doing double conversions but still far from perfect. You could go even further and multiply by another factor and add other values than five, thus allowing you to use right shifting instead of dividing by 10. The exact formula for doing this is left as an exercise to the reader. (Just google "divisions with Multiply Shift")
Have a nice day.
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.
I need to get the left hand side integer value from a decimal or double. For Ex: I need to get the value 4 from 4.6. I tried using Math.Floor function but it's returning a double value, for ex: It's returning 4.0 from 4.6. The MSDN documentation says that it returns an integer value. Am I missing something here? Or is there a different way to achieve what I'm looking for?
The range of double is much wider than the range of int or long. Consider this code:
double d = 100000000000000000000d;
long x = Math.Floor(d); // Invalid in reality
The integer is outside the range of long - so what would you expect to happen?
Typically you know that the value will actually be within the range of int or long, so you cast it:
double d = 1000.1234d;
int x = (int) Math.Floor(d);
but the onus for that cast is on the developer, not on Math.Floor itself. It would have been unnecessarily restrictive to make it just fail with an exception for all values outside the range of long.
According to MSDN, Math.Floor(double) returns a double: http://msdn.microsoft.com/en-us/library/e0b5f0xb.aspx
If you want it as an int:
int result = (int)Math.Floor(yourVariable);
I can see how the MSDN article can be misleading, they should have specified that while the result is an "integer" (in this case meaning whole number) it is still of TYPE Double
If you just need the integer portion of a number, cast the number to an int. This will truncate the number at the decimal point.
double myDouble = 4.6;
int myInteger = (int)myDouble;
Floor leaves it as a double so you can do more double calculations with it.
If you want it as an int, cast the result of floor as an int.
Don't cast the original double as an int because the rules for floor are different (IIRC) for negative numbers.
Convert.ToInt32(Math.Floor(Convert.ToDouble(value)))
This will give you the exact value what you want like if you take 4.6 it returns 4 as output.