I am doing: -
Decimal production = 0;
Decimal expense = 5000;
Decimal.ToUInt64(production - expense);
But it throws exception with the following error message.
"Value was either too large or too small for a UInt64"
Can someone give me a workaround for this.
Thanks!
Edit
In any case, I want the result as a positive number.
Problem: -5000m is a negative number, which is outside the range of
UInt64
(an unsigned type).Solution: use
Int64
instead ofUInt64
if you want to cope with negative numbers.Note that you can just cast instead of calling
Decimal.To...
:Alternative: validate that the number is non-negative before trying to convert it, and deal with it however you deem appropriate.
Very dodgy alternative: if you really just want the absolute value (which seems unlikely) you could use
Math.Abs
:UInt can not store negative numbers. The result of your calculation is negative. That's why the error comes. Check the sign before using ToUInt64 and correct it via *-1 or use a signed Int64.
0 - 5000 will return -5000. And you are trying to convert to an unsigned int which can not take negative values.
Try changing it to signed int
use