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));
Related
This question already has answers here:
How to increase value of an integer higher than 1 in one command?
(2 answers)
Closed 2 years ago.
I'd like to increment my var value of n, using a similar structure of the post-increment solution like var++.
The post-increment is like this:
int var = 0;
var++; // var = var + 1
I'd like to increment var of n, with n = 4 for example. What's the correct syntax ?
int var = 0;
var++4; // var = var + 4 but obviously not working;
what you are looking for is actually a Compound assignment with arithmetic operators
int a = 5;
a += 9;
Console.WriteLine(a); // output: 14
a -= 4;
Console.WriteLine(a); // output: 10
a *= 2;
Console.WriteLine(a); // output: 20
a /= 4;
Console.WriteLine(a); // output: 5
a %= 3;
Console.WriteLine(a); // output: 2
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);
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:
Is there an easy way to turn an int into an array of ints of each digit?
(11 answers)
Closed 7 years ago.
I want to know if there is a way in C# to convert an integer to an array of digits so that I can perform (Mathematical) operations on each digit alone.
Example: I need the user to input an integer i.e 123, 456
then the program creates two arrays of three elements {1,2,3}, {4,5,6}.
Off the top of my head:
int i = 123;
var digits = i.ToString().Select(t=>int.Parse(t.ToString())).ToArray();
You could create such array (or List) avoiding string operations as follows:
int x = 123;
List<int> digits = new List<int>();
while(x > 0)
{
int digit;
x = Math.DivRem(x, 10, out digit);
digits.Add(digit);
}
digits.Reverse();
Alternative without using the List and the List.Reverse:
int x = 456;
int[] digits = new int[1 + (int)Math.Log10(x)];
for (int i = digits.Length - 1; i >= 0; i--)
{
int digit;
x = Math.DivRem(x, 10, out digit);
digits[i] = digit;
}
And one more way using ToString:
int x = 123;
int[] digits = Array.ConvertAll(x.ToString("0").ToCharArray(), ch => ch - '0');
You can use this and not convert to a string:
var digits = new List<int>();
var integer = 123456;
while (integer > 0)
{
digits.Add(integer % 10);
integer /= 10;
}
digits.Reverse();
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;