简体   繁体   中英

If statement between ulong and 0

Im trying to do this calculation but its trying to convert a ulong to an int to see if its less than 0. Ive tried getting rid of int and putting it before the 0 instead but its not working at all.

ulong a = -897324782938287523942985;
ulong b = 95;
if ((int)(a + (b * 10000)) < 0) 
{
 a += (b*10000)
} else {
 a += b;
}

which gives the error:

Exception trown: 'System.OverflowException' in mscorlib.dll

I need it to give me back a positive number just over 0 and below 95

It throws an exception because the value of a is invalid. ulong means Unsigned Long — it doesn't accept negative numbers. And value 897324782938287523942985 is too big for all integer types in C# .

UPDATE : a should be of type double . Firstly, it accepts negative numbers, and secondly, it accepts very big values — 897324782938287523942985 is valid for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM