I know this will really turn out to be simple, but my brain is just not working. I need a function in C# that will return -1 if the integer passed to the function has a negative sign, return 1 if the integer has a positive sign and return 0 if the number passed is 0. So for example:
int Sign=SignFunction(-82); // Should return -1
int Sign2=SignFunction(197); // Should return 1
int Sign3=SignFunction(0); // Should return 0
This is already in the framework. Just use Math.Sign...
int sign = Math.Sign(-82); // Should return -1
int sign2 = Math.Sign(197); // Should return 1
int sign3 = Math.Sign(0); // Should return 0
In addition, it will work with:
int sign4 = Math.Sign(-5.2); // Double value, returns -1
int sign5 = Math.Sign(0.0m); // Decimal, returns 0
// ....
int sign = Math.Sign(number);
It already exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With