How to Check If a Number Is Positive or Negative in C#

How do I check if a number is positive or negative in C#?

bool positive = number > 0;
bool negative = number < 0;

Check for negative value with TryParse C#

Change && to ||. As || stops after first expression to be evaluated as true then:

  • If the input is not a double then it will iterate again (and won't check second condition as && would - which was the problem)
  • If the input is a double but is <= 0 it will iterate again.

Therefore it will stop iterating iff input is a positive double.

while (!double.TryParse(Console.ReadLine(), out radius) || radius <=0)

Also as you are using this code twice (at least) maybe encapsulate it in a function?

public double GetPositiveDouble()
{
double result;
while (!double.TryParse(Console.ReadLine(), out result))
{
Console.WriteLine("Wert ist ungültig! Bitte erneut versuchen.");
}
return result;
}

Check if a Negative number falls within a range

You should try following if range is -44 to -46.

 if (!(cameraTemp <= -44 && cameraTemp >= -46))

when we fall to nagative number like 0 to -1. 0 is greater then -1. Same way -1 greater then -2. So it is bit reverse order.

If you get confuse with this then you have to convert value to absolute value and then apply condition.

To elaborate more.

Following condition

1. if (!(cameraTemp <= -44 && cameraTemp >= -46))
{
// Some print or logic
}

If you write condition above way then your logic only execute if temperature value is not between -44 to -46 including -44 and -46.

2. if ((cameraTemp <= -44 && cameraTemp >= -46))
{
// Some print or logic
}
If you write this way then it only execute for value -44 , -45 and -46.

problem with if-statement and negative integer

It should work because int here is Int32 range is -2,147,483,648 to 2,147,483,647. If you are entering any number within this range, it should work. For number outside this range you will get "value too small or too large".
Please share the number you are entering

How to check if a boxed numeric value is negative?

It seems like the only way to do it.

But if you wanted to avoid writing multiple functions that take double, int, float, etc to check for negative number, I would redirect you to following post: https://stackoverflow.com/a/828820/150830

Check if a number only exist as positive or negated in ListListint efficiently

I've been playing with this idea, which so far seems to work. Of course I had to modify your data set slightly because as it was, no number was passing the test of being only either positive or negative.

So, here goes:

  //Setting up the mock data
List<List<int>> data = new List<List<int>>();
data.Add(new List<int>(new[] { 1, 3 }));
data.Add(new List<int>(new[] { -1, -3 }));
data.Add(new List<int>(new[] { 2, 5 }));
data.Add(new List<int>(new[] { -2, -5 }));
data.Add(new List<int>(new[] { -3, 4 }));
data.Add(new List<int>(new[] { -5, 4 }));
data.Add(new List<int>(new[] { 3, 5, -4 }));
data.Add(new List<int>(new[] { 6, -8 }));
data.Add(new List<int>(new[] { 7, -8 }));
data.Add(new List<int>(new[] { -6, -7, 8 }));
data.Add(new List<int>(new[] { 7, 9 }));
data.Add(new List<int>(new[] { -7, -9 }));
data.Add(new List<int>(new[] { 3, 8, -10 }));
data.Add(new List<int>(new[] { -3, -8, -10 }));
data.Add(new List<int>(new[] { -3, 8, 10 }));
data.Add(new List<int>(new[] { 3, -8, 10 }));
data.Add(new List<int>(new[] { 4, 9, -11 }));
data.Add(new List<int>(new[] { -4, -9, -11 }));
data.Add(new List<int>(new[] { -4, 9, 11 }));
data.Add(new List<int>(new[] { 4, -9, 11 }));
data.Add(new List<int>(new[] { 10, 11 }));
data.Add(new List<int>(new[] { -1, 6 }));
data.Add(new List<int>(new[] { 1, -6 }));
data.Add(new List<int>(new[] { -2, 7 }));
data.Add(new List<int>(new[] { 2, 39 }));

//Actual solution code
var all = data.SelectMany(l => l); //flatten
var grouped = all.GroupBy(g => Math.Abs(g)); //Group

//Look for groups where the Sum is equal Key * Count,
//which means only either all positives, or all negatives
var good = grouped.Where(i => i.Key * i.Count() == Math.Abs(i.Sum()));

var result = good.Select(g => g.First()); //Get rid of groups
var allPos = result.Where(i => i > 0); //Self explanatory
var allNeg = result.Where(i => i < 0);

I have split each step, but one could easily rewrite the linq stuff as one long query.

Cheers

how can I check if a string is a positive integer?

int x;
if (int.TryParse(str, out x) && x > 0)

How to validate negative string number

I assume you want to check if the number that you get back is a negative number?

double number = 0;
if(double.TryParse(myString,out number)){
if (number > 0)
\\Do something
}


Related Topics



Leave a reply



Submit