This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Division returns zero
(8 answers)
Closed 2 years ago.
I was doing a data science pet project, this is a simplified version of the problem:
int b = 102;
int c = 248;
double a = (b / c) * 100;
Console.WriteLine(a); // prints 0
This code prints zero for some reason. Any other alternatives? Why is this happening?
Because "b / c" is zero, you should use:
int b = 102;
int c = 248;
double a = ((double)b / c) * 100;
int k = Convert.ToInt32(a);
Console.WriteLine(k);
Related
This question already has answers here:
How can I divide two integers to get a double?
(9 answers)
Why does Decimal.Divide(int, int) work, but not (int / int)?
(8 answers)
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 1 year ago.
How do I insert decimal point into string?
int attrval = Convert.ToInt32("000504");
decimal val1 = (attrval / 100);
string val2 = String.Format("{0:.00}", attrval / 100);
val1 = 5 need 5.04
val2 = 5.00 need 5.04
You have an issue with integer division in the line
decimal val1 = (attrval / 100);
Since both attrval and 100 are of type int the result is int as well: 5. This line should be
decimal val1 = (attrval / 100m);
please, note m suffix: here we divide int by decimal (100m) and have a desired decimal result. Same for val2:
string val2 = String.Format("{0:.00}", attrval / 100m);
Division of two integer numbers return integer value as a result. This division rounds resultant value towards zero, that is why you are getting val2 = 5 instead of 5.04.
If you want result to be in decimal type, convert at-least one value either numerator or denominator to the decimal.
decimal val1 = (attrval / (decimal)100); //5.04
or
decimal val1 = (attrval / 100m); //5.04
Now convert it into string,
string val2 = String.Format("{0:.00}", var1); //"5.04"
Try it online
This question already has answers here:
I'm using ulong for the factorial of 100, but it still overflows
(2 answers)
Closed 3 years ago.
I have this code to determine factorial of 100. But after fifties it starts giving result of 0. I looked for answers for this problem and the common saying was using long variable. But even though using it, it still says that the answer is 0. Can you tell me where I am making mistake ?
static void Main(string[] args)
{
long c;
int a = 100;
c = a * (a - 1);
for (int i=a-2; i>=1 ; i--)
{
c = c * i;
}
Console.WriteLine(c);
Console.ReadLine();
}`
long type isn't long enough to store the factorial of 100.
Use BigInteger instead:
BigInteger c = new BigInteger(0);
int a = 100;
c = a * (a - 1);
for (int i = a - 2; i >= 1; i--)
{
c = c * i;
}
Console.WriteLine(c);
Console.ReadLine();
In order to use BigInteger you have to add a reference to the System.Numerics assembly to your project. (link)
This question already has answers here:
How to determine if a decimal/double is an integer?
(17 answers)
Get the decimal part from a double
(18 answers)
Closed 4 years ago.
I have a variable from:
double result = myList.Count / mySeptum;
I want to do the following:
if( result == int ) {
//Do Something...
}
else{
//Do another thing...
}
How can I do this?
I also tried this, but it didn't work:
if ( result%10 == 0 ){
...
}
In an example:
private void button2_Click(object sender, EventArgs e)
{
int r = 10;
int l = 2;
double d = r / l;
if (d % 10 == 0)
{
Console.WriteLine("INTEGER");
}
else
{
Console.WriteLine("DOUBLE");
}
}
For example:
double d = 1.0;
bool isInt = d == (int)d;
modulo:
double d = 1.0;
bool isInt = d % 1 == 0;
In general a floating point number on a computer can not represent every real number but only some discrete values. Thus, only for a few integers it will be possible that a double can be mathematically identical to an integer value. For most integers the closest double will be off by a small amount. So if you are looking for exact matches this will not work.
However, what you could do is to convert your double into an integer an check if the difference between the double and the integer is small enough:
double d = 1.5;
int i = (int) d;
double diff = d - i;
if (diff < 1.0e-6)
{
std::cout << "number is close to integer" << std::endl;
}
How to check my double variable is an integer or not?
From a C point of view (as post was originally tagged):
(I am certain C# has equivalent functions.)
To determine if a double is a whole number, use modf() to return the fractional part.
#include <math.h>
double x = ....;
double ipart;
if (isfinite(x) && modf(x, &ipart) == 0.0) {
// value is a whole number
....
To further test if it is in int range
if (ipart >= INT_MIN && ipart <= INT_MAX) {
int i = (int) ipart;
To check for wider integer types, we need some trickery to insure to no round-off error when forming the limits. Code takes advantage that INT..._MAX are Mersenne numbers
#define INT64_MAX_P1 ((INT64_MAX/2 + 1)*2.0)
if (ipart >= INT64_MIN && ipart < INT64_MAX_P1) {
int64_t i64 = (int64_t) ipart;
Try with typeOf:
if (myInt.GetType() == typeof(int))
This question already has answers here:
How to round up value C# to the nearest integer?
(10 answers)
Closed 6 years ago.
int a = 4;
int b = 3;
int c = a/b;
Here the c value as 1.33 . now i need to change the value as whole number like 2.
If,
int a =1;
int b = 2;
int c = a/b means the value is 0.5
now i need to change the value as next whole number like 1
Please try this:
int a = 4;
int b = 3;
int c = Convert.ToInt32(Math.Ceiling(a/b));
This question already has answers here:
What is the best practice to make division return double in C#
(6 answers)
Closed 8 years ago.
I'm trying to make a double divion with this code:
int a = 5;
int b = 5;
double result = (a + b) / 4;
How to get a 2.5 as a result and not 2, please?
Thanks in advance!
One of the arguments needs to be a double. So:
double result = (a + b) / 4.0;
Or
double result = (double)(a + b) / 4;